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

`:heading()` CSS pseudo-class

· 52CE 我爱测

The **`:heading()`** CSS pseudo-class function represents all heading elements whose levels match a comma-separated list of integers. This allows you to style elements at specific heading levels at once, rather than matching and styling them individually.

The :heading() CSS pseudo-class function represents all heading elements whose levels match a comma-separated list of integers. This allows you to style elements at specific heading levels at once, rather than matching and styling them individually.

[!NOTE] The :heading() functional pseudo-class has the same specificity as a class selector, that is, 0-1-0. For example, section:heading() has a specificity of 0-1-1.

Syntax

:heading( <integer># ) {
  /* ... */
}

Parameters

The :heading() pseudo-class function takes a comma-separated list of <integer>s that represent the heading levels to be styled.

Usage notes

The :heading() functional pseudo-class matches only elements that are semantically recognized as headings. It does not match elements that use role="heading" or 'aria-level' attributes.

The heading level used by :heading() may be different from an element's type selector in cases where the browser computes a different exposed heading level. For example, h1:heading(3) will match any <h1> element that is exposed as a level 3 heading.

Examples

Selecting specific heading levels

In this example, a comma-separated list of values is used to target headings at odd-numbered levels (<h1> and <h3>) and even-numbered levels (<h2> and <h4>).

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
:heading(1, 3) {
  color: tomato;
}
:heading(2, 4) {
  color: slateblue;
}

selecting_specific_heading_levels

Specifications

Browser compatibility

See also

  • :heading

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

资料来源与适用范围

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

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

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