<p>: The Paragraph element

<p> HTML 元素代表一个段落。段落通常在视觉媒体中表示为通过空行和/或首行缩进与相邻块分隔开的文本块,但 HTML 段落可以是相关内容的任何结构分组,例如图片或表单字段。

¥The <p> HTML element represents a paragraph. Paragraphs are usually represented in visual media as blocks of text separated from adjacent blocks by blank lines and/or first-line indentation, but HTML paragraphs can be any structural grouping of related content, such as images or form fields.

段落是 块级元素,特别是如果在结束 </p> 标记之前解析另一个块级元素,段落将自动关闭。参见下面的 "标签遗漏"。

¥Paragraphs are block-level elements, and notably will automatically close if another block-level element is parsed before the closing </p> tag. See "Tag omission" below.

Try it

属性

¥Attributes

该元素仅包含 全局属性

¥This element only includes the global attributes.

注意:<p> 标记上的 align 属性已过时,不应使用。

¥Note: The align attribute on <p> tags is obsolete and shouldn't be used.

示例

¥Examples

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>

结果

¥Result

段落样式

¥Styling paragraphs

默认情况下,浏览器用一个空行分隔段落。可以使用 CSS 实现替代分隔方法,例如一线缩进:

¥By default, browsers separate paragraphs with a single blank line. Alternate separation methods, such as first-line indentation, can be achieved with 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,
  ];
});

结果

¥Result

无障碍问题

¥Accessibility concerns

将内容分成段落有助于使页面更易于访问。屏幕阅读器和其他辅助技术提供了快捷方式,让用户跳到下一个或上一个段落,让他们浏览内容,就像空白让视觉用户跳过一样。

¥Breaking up content into paragraphs helps make a page more accessible. Screen-readers and other assistive technology provide shortcuts to let their users skip to the next or previous paragraph, letting them skim content like how white space lets visual users skip around.

对于使用屏幕阅读技术进行导航的人来说,使用空的 <p> 元素在段落之间添加空格是有问题的。屏幕阅读器可能会宣布该段落的存在,但不会宣布其中包含的任何内容 - 因为不存在任何内容。这可能会让使用屏幕阅读器的人感到困惑和沮丧。

¥Using empty <p> elements to add space between paragraphs is problematic for people who navigate with screen-reading technology. Screen readers may announce the paragraph's presence, but not any content contained within it — because there is none. This can confuse and frustrate the person using the screen reader.

如果需要额外的空间,请使用 CSS 属性(如 margin)来创建效果:

¥If extra space is desired, use CSS properties like margin to create the effect:

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

技术总结

¥Technical summary

内容类别 流量内容,可触及的内容。
允许的内容 措辞内容.
标签遗漏 开始标记是必需的。如果 <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

规范

Specification
HTML Standard
# the-p-element

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also