<p>: 段落元素

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

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

段落是 塊級元素,並且特別是在解析出另一個塊級元素(在閉合的 </p> 標籤之前)時,它們會自動閉合。請參見下面的“標籤省略”。

試一試

<p>
  Geckos are a group of usually small, usually nocturnal lizards. They are found
  on every continent except Antarctica.
</p>

<p>
  Some species live in houses where they hunt insects attracted by artificial
  light.
</p>
p {
  margin: 10px 0;
  padding: 5px;
  border: 1px solid #999999;
}

屬性

此元素僅包含全域性屬性

注意: <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 角色 段落
允許的 ARIA 角色 任意
DOM 介面 HTMLParagraphElement

規範

規範
HTML
# the-p-element

瀏覽器相容性

另見