<code>:行內程式碼元素
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
<code> HTML 元素以一種樣式顯示其內容,意在指示該文字是一小段電腦程式碼。預設情況下,內容文字以使用者代理的預設的等寬字體顯示。
嘗試一下
<p>
The <code>push()</code> method adds one or more elements to the end of an
array and returns the new length of the array.
</p>
code {
background-color: #eee;
border-radius: 3px;
font-family: courier, monospace;
padding: 0 3px;
}
屬性
此元素僅包括全域屬性。
範例
包含 <code> 的文字段落:
html
<p>
The function <code>selectAll()</code> highlights all the text in the input
field so the user can, for example, copy or delete the text.
</p>
結果
備註
要表示多行程式碼,請將 <code> 元素包裹在 <pre> 元素內。單獨使用 <code> 元素只能表示單個程式碼片語或程式碼行。
可以定義 CSS 規則以覆蓋瀏覽器的預設字型。使用者設定的偏好可能優先於指定的 CSS。
技術摘要
| 內容類型 | 流內容、段落型內容、捫及內容。 |
|---|---|
| 允許的內容 | 段落型內容。 |
| 標籤省略 | 不允許,開始和結束標籤都是必須的。 |
| 允許的父元素 | 任何接受段落型內容的元素。 |
| 隱含的 ARIA 角色 |
code
|
| 允許的 ARIA 角色 | 任何 |
| DOM 介面 |
HTMLElement 在 Gecko 1.9.2(Firefox 4)及以前版本中,Firefox 為這個元素實現了 HTMLSpanElement 介面。
|
規範
| Specification |
|---|
| HTML> # the-code-element> |
瀏覽器相容性
Loading…