メインコンテンツへスキップ

pre要素


1.仕様

PRE要素は、
整形済みテキストを
表します。

pre

整形済みテキスト (preformatted text)

種類

通常要素

カテゴリ

使用できるコンテキスト
フローコンテンツを受け入れる要素

フローコンテンツを受け入れる要素のコンテンツとして、使用できます。

フローコンテンツを受け入れる要素は、body要素, section要素, nav要素, aside要素, article要素, header要素, footer要素, main要素, address要素, li要素, dt要素, dd要素, caption要素, th要素, td要素, form要素, fieldset要素, search要素, blockquote要素, figure要素, figcaption要素, div要素, dialog要素, details要素 です。

タグの省略

どちらのタグも省略できません。

コンテンツ属性

グローバル属性のみ

DOM インターフェイス
HTMLPreElement
  1. [Exposed=Window]
  2. interface HTMLPreElement : HTMLElement {
  3. [HTMLConstructor] constructor();
  4.  
  5. // also has obsolete members
  6. };
HTMLElement
  1. [Exposed=Window]
  2. interface HTMLElement : Element {
  3. [HTMLConstructor] constructor();
  4.  
  5. // metadata attributes
  6. [CEReactions] attribute DOMString title;
  7. [CEReactions] attribute DOMString lang;
  8. [CEReactions] attribute boolean translate;
  9. [CEReactions] attribute DOMString dir;
  10.  
  11. // user interaction
  12. [CEReactions] attribute (boolean or unrestricted double or DOMString)? hidden;
  13. [CEReactions] attribute DOMString inert;
  14. undefined click();
  15. [CEReactions] attribute DOMString accessKey;
  16. readonly attribute DOMString accessKeyLabel;
  17. [CEReactions] attribute boolean draggable;
  18. [CEReactions] attribute boolean spellcheck;
  19. [CEReactions] attribute DOMString writingSuggestions;
  20. [CEReactions] attribute DOMString autocapitalize;
  21.  
  22. [CEReactions] attribute [LegacyNullToEmptyString] DOMString innerText;
  23. [CEReactions] attribute [LegacyNullToEmptyString] DOMString outerText;
  24.  
  25. ElementInternals attachInternals();
  26.  
  27. // The popover API
  28. undefined showPopover();
  29. undefined hidePopover();
  30. boolean togglePopover(optional boolean force);
  31. [CEReactions] attribute DOMString? popover;
  32. };
  33.  
  34. HTMLElement includes GlobalEventHandlers;
  35. HTMLElement includes ElementContentEditable;
  36. HTMLElement includes HTMLOrSVGElement;
Element
  1. [Exposed=Window]
  2. interface Element : Node {
  3. readonly attribute DOMString? namespaceURI;
  4. readonly attribute DOMString? prefix;
  5. readonly attribute DOMString localName;
  6. readonly attribute DOMString tagName;
  7.  
  8. [CEReactions] attribute DOMString id;
  9. [CEReactions] attribute DOMString className;
  10. [SameObject, PutForwards=value] readonly attribute DOMTokenList classList;
  11. [CEReactions, Unscopable] attribute DOMString slot;
  12.  
  13. boolean hasAttributes();
  14. [SameObject] readonly attribute NamedNodeMap attributes;
  15. sequence<DOMString> getAttributeNames();
  16. DOMString? getAttribute(DOMString qualifiedName);
  17. DOMString? getAttributeNS(DOMString? namespace, DOMString localName);
  18. [CEReactions] undefined setAttribute(DOMString qualifiedName, DOMString value);
  19. [CEReactions] undefined setAttributeNS(DOMString? namespace, DOMString qualifiedName, DOMString value);
  20. [CEReactions] undefined removeAttribute(DOMString qualifiedName);
  21. [CEReactions] undefined removeAttributeNS(DOMString? namespace, DOMString localName);
  22. [CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
  23. boolean hasAttribute(DOMString qualifiedName);
  24. boolean hasAttributeNS(DOMString? namespace, DOMString localName);
  25.  
  26. Attr? getAttributeNode(DOMString qualifiedName);
  27. Attr? getAttributeNodeNS(DOMString? namespace, DOMString localName);
  28. [CEReactions] Attr? setAttributeNode(Attr attr);
  29. [CEReactions] Attr? setAttributeNodeNS(Attr attr);
  30. [CEReactions] Attr removeAttributeNode(Attr attr);
  31.  
  32. ShadowRoot attachShadow(ShadowRootInit init);
  33. readonly attribute ShadowRoot? shadowRoot;
  34.  
  35. Element? closest(DOMString selectors);
  36. boolean matches(DOMString selectors);
  37. boolean webkitMatchesSelector(DOMString selectors); // legacy alias of .matches
  38.  
  39. HTMLCollection getElementsByTagName(DOMString qualifiedName);
  40. HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
  41. HTMLCollection getElementsByClassName(DOMString classNames);
  42.  
  43. [CEReactions] Element? insertAdjacentElement(DOMString where, Element element); // legacy
  44. undefined insertAdjacentText(DOMString where, DOMString data); // legacy
  45. };
GlobalEventHandlers
  1. interface mixin GlobalEventHandlers {
  2. attribute EventHandler onabort;
  3. attribute EventHandler onauxclick;
  4. attribute EventHandler onbeforeinput;
  5. attribute EventHandler onbeforematch;
  6. attribute EventHandler onbeforetoggle;
  7. attribute EventHandler onblur;
  8. attribute EventHandler oncancel;
  9. attribute EventHandler oncanplay;
  10. attribute EventHandler oncanplaythrough;
  11. attribute EventHandler onchange;
  12. attribute EventHandler onclick;
  13. attribute EventHandler onclose;
  14. attribute EventHandler oncontextlost;
  15. attribute EventHandler oncontextmenu;
  16. attribute EventHandler oncontextrestored;
  17. attribute EventHandler oncopy;
  18. attribute EventHandler oncuechange;
  19. attribute EventHandler oncut;
  20. attribute EventHandler ondblclick;
  21. attribute EventHandler ondrag;
  22. attribute EventHandler ondragend;
  23. attribute EventHandler ondragenter;
  24. attribute EventHandler ondragleave;
  25. attribute EventHandler ondragover;
  26. attribute EventHandler ondragstart;
  27. attribute EventHandler ondrop;
  28. attribute EventHandler ondurationchange;
  29. attribute EventHandler onemptied;
  30. attribute EventHandler onended;
  31. attribute OnErrorEventHandler onerror;
  32. attribute EventHandler onfocus;
  33. attribute EventHandler onformdata;
  34. attribute EventHandler oninput;
  35. attribute EventHandler oninvalid;
  36. attribute EventHandler onkeydown;
  37. attribute EventHandler onkeypress;
  38. attribute EventHandler onkeyup;
  39. attribute EventHandler onload;
  40. attribute EventHandler onloadeddata;
  41. attribute EventHandler onloadedmetadata;
  42. attribute EventHandler onloadstart;
  43. attribute EventHandler onmousedown;
  44. [LegacyLenientThis] attribute EventHandler onmouseenter;
  45. [LegacyLenientThis] attribute EventHandler onmouseleave;
  46. attribute EventHandler onmousemove;
  47. attribute EventHandler onmouseout;
  48. attribute EventHandler onmouseover;
  49. attribute EventHandler onmouseup;
  50. attribute EventHandler onpaste;
  51. attribute EventHandler onpause;
  52. attribute EventHandler onplay;
  53. attribute EventHandler onplaying;
  54. attribute EventHandler onprogress;
  55. attribute EventHandler onratechange;
  56. attribute EventHandler onreset;
  57. attribute EventHandler onresize;
  58. attribute EventHandler onscroll;
  59. attribute EventHandler onscrollend;
  60. attribute EventHandler onsecuritypolicyviolation;
  61. attribute EventHandler onseeked;
  62. attribute EventHandler onseeking;
  63. attribute EventHandler onselect;
  64. attribute EventHandler onslotchange;
  65. attribute EventHandler onstalled;
  66. attribute EventHandler onsubmit;
  67. attribute EventHandler onsuspend;
  68. attribute EventHandler ontimeupdate;
  69. attribute EventHandler ontoggle;
  70. attribute EventHandler onvolumechange;
  71. attribute EventHandler onwaiting;
  72. attribute EventHandler onwebkitanimationend;
  73. attribute EventHandler onwebkitanimationiteration;
  74. attribute EventHandler onwebkitanimationstart;
  75. attribute EventHandler onwebkittransitionend;
  76. attribute EventHandler onwheel;
  77. };
ElementContentEditable
  1. interface mixin ElementContentEditable {
  2. [CEReactions] attribute DOMString contentEditable;
  3. [CEReactions] attribute DOMString enterKeyHint;
  4. readonly attribute boolean isContentEditable;
  5. [CEReactions] attribute DOMString inputMode;
  6. };
HTMLOrSVGElement
  1. interface mixin HTMLOrSVGElement {
  2. [SameObject] readonly attribute DOMStringMap dataset;
  3. attribute DOMString nonce; // intentionally no [CEReactions]
  4.  
  5. [CEReactions] attribute boolean autofocus;
  6. [CEReactions] attribute long tabIndex;
  7. undefined focus(optional FocusOptions options = {});
  8. undefined blur();
  9. };
ARIA in HTML
暗黙的="generic" ARIAロール="任意" aria-* 属性="GlobalAny"

暗黙的な ARIAセマンティクスは、role=generic です。

要素で任意の role 値を使用できます。ただし、作成者が要素の暗黙的なロール, 汎用ロール, または ARIA によって非推奨となったロールをこれらの要素に対して指定することは推奨されません。

グローバル aria-* 属性と、許可されたロールに適用される任意の aria-* 属性が記述できます。
NamingProhibited

レンダリング
ブロックボックス
フローコンテンツ
  1. pre {
  2. pre {
  3. display: block;
  4. margin-block: 1em;
  5. font-family: monospace;
  6. white-space: pre;
  7. unicode-bidi: isolate;
  8. }
  9. pre[wrap] {
  10. white-space: pre-wrap;
  11. }
  12. }

CSS を大雑把に読むと、 ブロックレベルのブロックコンテナボックス(ブロックボックス)を生成します。 などになります。

説明
pre要素は、構造が要素ではなく印刷規則によって表される、整形済みのテキストのブロックを表します。
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>pre要素の例 - (Example of pre element) | 離珠のHTMLでしぃ!</title>
  6. </head>
  7. <body>
  8. <h1>pre要素の例</h1>
  9. <pre>
  10. pre {
  11. display: block;
  12. margin-block: 1em;
  13. font-family: monospace;
  14. white-space: pre;
  15. }
  16. </pre>
  17. </body>
  18. </html>

このサンプルへのリンクです。
(./expre.htm)

JSONデータ
JSON形式での、データのイメージです。
  1. {
  2. "pre": {
  3. "check": "2024-09-01",
  4. "group": "Flow",
  5. "element": "- - (%Phrasing)* -- preformatted text. (Normal elements.) [Flow|Palpable] --",
  6. "attlist": {"%Global;": ""},
  7. "comment": "整形済みテキスト (preformatted text)",
  8. "point": "構造が要素ではなく印刷規則によって表される、整形済みのテキストのブロックを表します。",
  9. "kind": "Normal",
  10. "categories": [
  11. "Flow",
  12. "Palpable",
  13. ],
  14. "used": "parent %Flow",
  15. "contentmodel": "pre (%Phrasing)*",
  16. "tag": [
  17. "-",
  18. "-",
  19. ],
  20. "attributes": "Global",
  21. "dominterface": "HTMLPreElement",
  22. "rendering": [
  23. "Flow",
  24. "Bidirectional",
  25. ],
  26. "reference": [
  27. {
  28. "title": "HTML Living Standard. 4.4.3 The pre element.",
  29. "href": "https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element",
  30. "heading": "HTML Living Standard. 4.4.3 The pre element",
  31. "sub": "Last Updated 27 August 2024",
  32. },
  33. {
  34. "title": "HTML Living Standard. 15 Rendering 15.3.3 Flow content.",
  35. "href": "https://html.spec.whatwg.org/multipage/rendering.html#flow-content-3",
  36. "heading": "HTML Living Standard. 15 Rendering 15.3.3 Flow content",
  37. "sub": "Last Updated 27 August 2024",
  38. },
  39. {
  40. "title": "HTML 5.2 W3C Recommendation. 4.4.4 The pre element. W3C Recommendation, 14 December 2017.",
  41. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/grouping-content.html#the-pre-element",
  42. "heading": "HTML 5.2 W3C 勧告. 4.4.4 The pre element",
  43. "sub": "W3C勧告、2017年12月14日",
  44. },
  45. {
  46. "title": "HTML 5.2 W3C Recommendation. 10.3.3. Flow content.",
  47. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/rendering.html#non-replaced-elements-flow-content",
  48. "heading": "HTML 5.2 W3C 勧告. 10.3.3. Flow content.",
  49. "sub": "W3C勧告、2017年12月14日",
  50. },
  51. {
  52. "title": "HTML 5.1 W3C Recommendation. 4.4.3 The pre element. W3C Recommendation, 1 November 2016.",
  53. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/grouping-content.html#the-pre-element",
  54. "heading": "HTML 5.1 W3C 勧告. 4.4.3 The pre element",
  55. "sub": "W3C勧告、2016年11月1日",
  56. },
  57. {
  58. "title": "HTML 5.1 W3C Recommendation. 10.3.3. Flow content. W3C Recommendation, 1 November 2016.",
  59. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/rendering.html#non-replaced-elements-flow-content",
  60. "heading": "HTML 5.1 W3C 勧告. 10.3.3. Flow content.",
  61. "sub": "W3C勧告、2016年11月1日",
  62. },
  63. {
  64. "title": "HTML 5 W3C Recommendation. 4.4.3 The pre element. W3C Recommendation, 28 October 2014.",
  65. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/grouping-content.html#the-pre-element",
  66. "heading": "HTML 5 W3C 勧告. 4.4.3 The pre element",
  67. "sub": "W3C勧告、2014年10月28日",
  68. },
  69. {
  70. "title": "HTML 5 W3C Recommendation. 10.3.3. Flow content. W3C Recommendation, 28 October 2014.",
  71. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/rendering.html#flow-content-0",
  72. "heading": "HTML 5 W3C 勧告. 10.3.3. Flow content.",
  73. "sub": "W3C勧告、2014年10月28日",
  74. },
  75. {
  76. "title": "HTML 4.01 Specification W3C Recommendation. 9.3.4 Preformatted text: The PRE element. W3C Recommendation 24 December 1999.",
  77. "href": "https://www.w3.org/TR/html401/struct/text.html#h-9.3.4",
  78. "heading": "HTML 4.01 Specification W3C 勧告. 9.3.4 Preformatted text: The PRE element",
  79. "sub": "W3C勧告1999年12月24日",
  80. },
  81. {
  82. "title": "HTML 3.2 Reference Specification. W3C Recommendation 14-Jan-1997.",
  83. "href": "https://www.w3.org/TR/2018/SPSD-html32-20180315/#pre",
  84. "heading": "HTML 3.2 Reference Specification",
  85. "sub": "W3C勧告1997年1月14日",
  86. },
  87. {
  88. "title": "Hypertext Markup Language - 2.0. November 1995.",
  89. "href": "https://www.ietf.org/rfc/rfc1866.txt",
  90. "heading": "Hypertext Markup Language - 2.0",
  91. "sub": "November 1995",
  92. },
  93. {
  94. "title": "Hypertext Markup Language (HTML). June 1993.",
  95. "href": "https://www.w3.org/MarkUp/draft-ietf-iiir-html-01.txt",
  96. "heading": "Hypertext Markup Language (HTML)",
  97. "sub": "June 1993",
  98. },
  99. {
  100. "title": "MDN Web Docs. HTML <pre>: The Preformatted Text element.",
  101. "href": "https://developer.mozilla.org/ja/docs/Web/HTML/Element/pre",
  102. "heading": "<pre>: 整形済みテキスト要素 - HTML: ハイパーテキストマークアップ言語 | MDN",
  103. },
  104. ],
  105. "version": {
  106. "first": "HTML",
  107. "latest": "HTML Living Standard",
  108. },
  109. }
  110. }

注意: 簡易に描画する為に、各項目の後ろに , を付与しています。JSON の形式では、末尾の項目の後には , は付けません。

最終確認日

2024-09-01


2.使用可能な箇所

フローコンテンツ
受け入れる要素に
配置できます。

pre要素を使用できる状態(コンテキストContext)は、 フローコンテンツを受け入れる要素に配置できます。

フローコンテンツを受け入れる要素は、body要素, section要素, nav要素, aside要素, article要素, header要素, footer要素, main要素, address要素, li要素, dt要素, dd要素, caption要素, th要素, td要素, form要素, fieldset要素, search要素, blockquote要素, figure要素, figcaption要素, div要素, dialog要素, details要素 です。


3.配置できるコンテンツ


4.属性

グローバル属性のみ、
設定できます。

pre要素は、グローバル属性のみ 設定できます。


5.Web API

pre要素を
直接表す
プロパティは
ありません。

pre要素を表すプロパティはないので、id属性を用いて documentgetElementById()メソッドで、要素を取得する方法がお勧めです。この時に取得できるのはHTMLPreElement オブジェクトです。pre要素id属性があれば、スクリプトで何か扱っているとHTMLソースだけを見ても推測できて注意を払えるからです。 尚、アンカーとしてid属性を用いる事も想定して、命名規則を決めておくのも良いと思います。


6.レンダリング

  1. pre {
  2. display: block;
  3. margin-block: 1em;
  4. font-family: monospace;
  5. white-space: pre;
  6. unicode-bidi: isolate;
  7. }

pre要素は、ブロックレベルのブロックコンテナであるブロックボックスを生成します。

pre要素の特徴としては、white-spaceプロパティpreとされ、整形されるテキストであるという事だと思います。


7.気を付ける点

整形

整形済みと表したい意図は何かと意識すると良いと思います。ただ装飾するだけなら、スタイルシートで出来る訳ですから。
尚、コンテンツモデルはフレージングコンテンツなので、フレージングコンテンツでないフローコンテンツな要素をまとめて整形済みとする事は出来ません。コードリストを行を意識してリストで表すなどフローコンテンツの要素を用いたい場合にはpre要素は不向きです。まぁ各行毎にpre要素で表すという考えもありますが、整形についてはスタイルシートで行うとした方が良いと思います。