<p>: 段落元素

基線 廣泛可用

此功能已經完善,並且可以在許多裝置和瀏覽器版本上執行。它自以下時間起在所有瀏覽器中都可用: 2015 年 7 月.

<p> 元素表示一個段落。在視覺媒體中,段落通常以文字塊的形式呈現,並用空行和/或首行縮排與相鄰的文字塊分隔,但 HTML 段落可以是任何相關的結構化內容分組,例如影像或表單欄位。

段落是 塊級元素,值得注意的是,如果在閉合標籤 </p> 之前解析了另一個塊級元素,則段落會自動閉合。請參閱下面的“標籤省略”。

試一試

屬性

此元素僅包含 全域性屬性

注意:<p> 標籤上的 align 屬性已過時,不應使用。

無障礙訪問

將內容分解成段落有助於使頁面更易於訪問。螢幕閱讀器和其他輔助技術提供快捷方式,讓使用者可以跳到下一個或上一個段落,讓他們像視覺使用者利用空白一樣瀏覽內容。

使用空 <p> 元素在段落之間新增空格對於使用螢幕閱讀器技術的使用者來說是有問題的。螢幕閱讀器可能會宣佈段落的存在,但不會宣佈其中包含的任何內容——因為沒有內容。這可能會讓使用螢幕閱讀器的人感到困惑和沮喪。

如果需要額外的空格,請使用 CSS 屬性,例如 margin 來建立效果。

css
p {
  margin-bottom: 2em; /* increase white space after a paragraph */
}

示例

HTML

html
<p>
  This is the first paragraph of text. This is the first paragraph of text. This
  is the first paragraph of text. This is the first paragraph of text.
</p>
<p>
  This is the second paragraph. This is the second paragraph. This is the second
  paragraph. This is the second paragraph.
</p>

結果

樣式化段落

預設情況下,瀏覽器會用單行空白分隔段落。可以使用 CSS 實現其他分隔方法,例如首行縮排。

HTML

html
<p>
  Separating paragraphs with blank lines is easiest for readers to scan, but
  they can also be separated by indenting their first lines. This is often used
  to take up less space, such as to save paper in print.
</p>

<p>
  Writing that is intended to be edited, such as school papers and rough drafts,
  uses both blank lines and indentation for separation. In finished works,
  combining both is considered redundant and amateurish.
</p>

<p>
  In very old writing, paragraphs were separated with a special character: ¶,
  the <i>pilcrow</i>. Nowadays, this is considered claustrophobic and hard to
  read.
</p>

<p>
  How hard to read? See for yourself:
  <button data-toggle-text="Oh no! Switch back!">
    Use pilcrow for paragraphs
  </button>
</p>

CSS

css
p {
  margin: 0;
  text-indent: 3ch;
}

p.pilcrow {
  text-indent: 0;
  display: inline;
}
p.pilcrow + p.pilcrow::before {
  content: " ¶ ";
}

JavaScript

js
document.querySelector("button").addEventListener("click", (event) => {
  document.querySelectorAll("p").forEach((paragraph) => {
    paragraph.classList.toggle("pilcrow");
  });

  [event.target.innerText, event.target.dataset.toggleText] = [
    event.target.dataset.toggleText,
    event.target.innerText,
  ];
});

結果

技術摘要

內容類別 流內容,可感知內容。
允許的內容 短語內容.
標籤省略 開始標籤是必需的。如果 <p> 元素緊跟在 <address><article><aside><blockquote><details><div><dl><fieldset><figcaption><figure><footer><form>h1h2h3h4h5h6<header><hgroup><hr><main><menu><nav><ol><pre><search><section><table><ul> 或另一個 <p> 元素之後,或者如果父元素中沒有更多內容並且父元素不是 <a><audio><del><ins><map><noscript><video> 元素,或者不是一個自主的自定義元素,則可以省略結束標籤。
允許的父元素 任何接受 流內容 的元素。
隱式 ARIA 角色 paragraph
允許的 ARIA 角色 任何
DOM 介面 HTMLParagraphElement

規範

規範
HTML 標準
# the-p-element

瀏覽器相容性

BCD 表格僅在啟用 JavaScript 的瀏覽器中載入。

另請參閱