HTMLタグ <PRE> は、開始タグと終了タグに挟まれたテキストが整形済みテキスト (Preformatted Text) であることを表します。
整形済みテキストの特徴を以下に示します。
<pre>
<!-- phrasing content -->
</pre>
pre 要素の開始タグと終了タグの間にフレージング・コンテンツを含めることができる。
属性は全て省略可能です。
<pre class="ma-4">text</pre>
<pre id="example">text</pre>
<pre lang="ja">text</pre>
<pre style="margin-top: 1rem;">text</pre>
<pre title="example">text</pre>
<pre>
if (flag == true) {
ret = true;
}
</pre>
if (flag == true) { ret = true; }
空白は纏められず、そのまま表示される。
<pre>This is a pen.</pre>
<div>This is a pen.</prdive>
This is a pen.
改行はそのまま改行される。
<pre>This
is
a
pen.</pre>
<div>This
is
a
pen.</div>
This is a pen.
JavaScriptからはHTMLPreElementインタフェースを通じてpre要素へアクセスできる。HTMLPreElementインタフェースは次に示すプロパティを持つ。
<pre id="example">text</pre>
<script>
document.getElementById("example").style = "margin: 1rem"
</script>
Web Hypertext Application Technology Working Group (2022) "Grouping content" HTML Living Standard