<p>: 段落元素
<p> 元素表示一個段落。在視覺媒體中,段落通常以文字塊的形式呈現,並用空行和/或首行縮排與相鄰的文字塊分隔,但 HTML 段落可以是任何相關的結構化內容分組,例如影像或表單欄位。
段落是 塊級元素,值得注意的是,如果在閉合標籤 </p> 之前解析了另一個塊級元素,則段落會自動閉合。請參閱下面的“標籤省略”。
試一試
屬性
此元素僅包含 全域性屬性。
注意:<p> 標籤上的 align 屬性已過時,不應使用。
無障礙訪問
示例
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>、h1、h2、h3、h4、h5、h6、<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 的瀏覽器中載入。