<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

浏览器兼容性

另见