<pre>:預格式化文字元素
試一試
屬性
此元素僅包含全域性屬性。
width已棄用 非標準-
包含一行首選字元數。雖然在技術上仍然實現,但此屬性沒有視覺效果;要實現此效果,請改用 CSS
width。 wrap非標準 已棄用-
是一個提示,指示溢位如何發生。在現代瀏覽器中,此提示被忽略,並且不會產生任何視覺效果;要實現此效果,請改用 CSS
white-space。
無障礙訪問
為使用預格式化文字建立的任何影像或圖表提供替代描述非常重要。替代描述應清楚簡潔地描述影像或圖表的內容。
視力障礙者和使用螢幕閱讀器等輔助技術的瀏覽者在按順序讀取預格式化文字字元時可能無法理解它們所代表的內容。
結合使用<figure>和<figcaption>元素,並補充pre元素上的ARIArole和aria-label屬性,可以將預格式化的ASCII藝術宣佈為帶替代文字的影像,figcaption用作影像的標題。
示例
html
<figure>
<pre role="img" aria-label="ASCII COW">
___________________________
< I'm an expert in my field. >
---------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
</pre>
<figcaption id="cow-caption">
A cow saying, "I'm an expert in my field." The cow is illustrated using
preformatted text characters.
</figcaption>
</figure>
示例
基本示例
HTML
html
<p>Using CSS to change the font color is easy.</p>
<pre><code>
body {
color: red;
}
</code></pre>
結果
轉義保留字元
HTML
html
<pre><code>
let i = 5;
if (i < 10 && i > 0)
return "Single Digit Number"
</code></pre>
結果
技術總結
規範
| 規範 |
|---|
| HTML 標準 # the-pre-element |
瀏覽器相容性
BCD 表格僅在瀏覽器中載入
另請參閱
- CSS:
white-space、word-break - 字元引用
- 相關元素:
<code>、<samp>、<kbd>