大网数据广告活动
Web 标准与前端开发

`:heading` CSS pseudo-class

· 52CE 我爱测

The **`:heading`** CSS pseudo-class matches all heading elements in a document.

The :heading CSS pseudo-class matches all heading elements in a document.

Syntax

:heading {
  /* ... */
}

Description

The :heading pseudo-class allows you to style all headings at once, rather than matching and styling them individually.

This pseudo-class matches only elements that by default are semantically recognized as headings (<h1> through <h6>). Elements with role="heading" are not matched; you can select those by using the [role="heading"] attribute selector.

The :heading pseudo-class has the same specificity as a class selector, that is, 0-1-0. So :heading has a specificity of 0-1-0, whereas h1, h2, h3, h4, h5, h6 would have a specificity of 0-0-1 and section:heading would have a specificity of 0-1-1.

Examples

Styling all headings

In this example, we use the :heading pseudo-class to style multiple levels of headings.

HTML

The document contains headings at three different levels, along with p paragraph elements.

<h1>Mastering CSS</h1>
<h2>Chapter 1: Selectors</h2>
<p>
  A CSS selector is the part of a CSS rule that describes what elements in a
  document the rule will match.
</p>
<h3>1.1 Pseudo-classes</h3>
<p>
  CSS pseudo-classes enable selecting elements based on information that lies
  outside of the document tree.
</p>

CSS

We set the heading elements to be italic and tomato red.

:heading {
  color: tomato;
  font-style: italic;
}

Results

styling_all_headings

The :heading pseudo-class applies the color and font-style to all the headings in the document, but not the paragraphs:

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。