<code>はプログラムのコードであることを表すHTMLタグです。プログラムのコードは等幅フォントで表示されます。
HTML要素のタグ名、属性名および属性値は、大文字と小文字のどちらでもよい。code 要素の終了タグは省略できない。
<code>
<!-- phrasing content -->
</code>
<CODE>
<!-- phrasing content -->
</CODE>
HTMLタグ <code> の使用例と表示サンプルを次に示します。
<code>printf("Hello World");</code>
printf("Hello World");
改行を含む複数行のプログラム・コードを表示するには、pre 要素と code 要素を組み合わせる。
<pre><code>body {
color: gray;
}</code></pre>
body {
color: gray;
}
code 要素の開始タグと終了タグの間にフレージング・コンテンツを含めることができる。
次に示す属性をcode要素に指定できる。
<code style="color: gray">let e = document.getElementById('example');</code>
let e = document.getElementById('example');
JavaScriptからは HTMLElement インタフェースを通じて code 要素へアクセスできる。
プロパティ | 型 | 説明 |
---|---|---|
accessKey | string | accessKey属性 |
accessKeyLabel | string | 要素に割り当てられたアクセスキー(読取り専用) |
contentEditable | string | contenteditable属性 |
isContentEditable | boolean | この要素が編集できるかどうか(読取り専用) |
dataset | DOMStringMap | カスタムデータ属性 |
innerText | string | ノードに描画されるテキスト |
document.querySelector("code").dir = "ltr";
document.querySelector("code").draggable = false;
document.querySelector("code").lang = "en";
document.querySelector("code").style = "color: gray";
document.querySelector("code").tabIndex = 0;
document.querySelector("code").title = "JavaScript";
Web Hypertext Application Technology Working Group (2023) Text-level semantics HTML Living Standard