Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c3ebe9a
added the :heading and :heading pages
dletorey Aug 5, 2025
c06d7bc
Merge branch 'main' into 40483-heading-selectors
dletorey Aug 6, 2025
dd69d01
finshed the :heading & :heading() pages
dletorey Aug 6, 2025
169f663
added note to :heading() and added both to pseudo-classes page
dletorey Aug 6, 2025
af829de
Merge branch 'main' into 40483-heading-selectors
dletorey Aug 6, 2025
10ceb0a
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 6, 2025
e96bf2a
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 6, 2025
984b927
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 6, 2025
917d7f8
made explanation match code example
dletorey Aug 6, 2025
531fb9f
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
77ac648
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
0630303
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
d2d981e
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
a862da4
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
396f6b5
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
70af4d7
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
2b93476
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 11, 2025
1030607
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 11, 2025
f258db7
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 11, 2025
d587432
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 11, 2025
96c21f7
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 11, 2025
2667e9e
Merge branch 'main' into 40483-heading-selectors
dletorey Aug 11, 2025
1248fc1
fixed code blocks
dletorey Aug 11, 2025
faa1fc0
Merge branch 'main' into 40483-heading-selectors
dletorey Aug 11, 2025
ba72433
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 11, 2025
1cb2752
updated the Note & parameters
dletorey Aug 11, 2025
9944d0c
Merge branch '40483-heading-selectors' of github.com:dletorey/content…
dletorey Aug 11, 2025
0c962f8
fixed the syntax example
dletorey Aug 11, 2025
1ef85b8
added a note about matching attributes
dletorey Aug 11, 2025
8070f80
moved the note to usage notes
dletorey Aug 11, 2025
bf5bdc8
Merge branch 'main' into 40483-heading-selectors
dletorey Aug 11, 2025
a7d3ed4
Merge branch 'main' into 40483-heading-selectors
dletorey Aug 11, 2025
714d935
Merge branch 'main' into 40483-heading-selectors
caugner Aug 11, 2025
c5f8d28
Update files/en-us/web/css/_colon_heading/index.md
dletorey Aug 18, 2025
0979834
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 18, 2025
0ed3a0f
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 18, 2025
a2bce13
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 18, 2025
1587bc1
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 18, 2025
6104a30
Update files/en-us/web/css/_colon_heading_function/index.md
dletorey Aug 18, 2025
509583b
Merge branch 'main' into 40483-heading-selectors
dletorey Aug 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions files/en-us/web/css/_colon_heading/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: :heading
slug: Web/CSS/:heading
page-type: css-pseudo-class
browser-compat: css.selectors.heading
sidebar: cssref
---

The **`:heading`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) matches all [heading elements](/en-US/docs/Web/HTML/Reference/Elements/Heading_Elements) in a document. This allows you to style all headings at once, rather than matching and styling them individually.
This pseudo-class matches only elements that are semantically recognized as headings (`<h1>` through `<h6>`). Elements with [`role="heading"`](/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/heading_role) are not matched; you can select those by using the functional form, {{CSSXRef(":heading_function", ":heading()")}}.

> [!NOTE]
> The `:heading` pseudo-class has the same [specificity](/en-US/docs/Web/CSS/CSS_cascade/Specificity#how_is_specificity_calculated) as a class selector, that is, `0-1-0`. So `:heading` would have a specificity of `0-1-0`, and `section:heading` would have a specificity of `0-1-1`.

## Syntax

```css
:heading {
/* ... */
}
```

## Examples

### Styling all headings

The document in this example contains headings at three different levels.

```html
<h1>Mastering CSS</h1>
<h2>Chapter 1: Selectors</h2>
<h3>1.1 Pseudo-classes</h3>
```

```css
:heading {
color: tomato;
}
```
The `:heading` pseudo-class applies the `color` to all the headings in the document:

{{EmbedLiveSample("styling_all_headings", "", "170")}}

The `:heading` pseudo-class applies the `color` to all the headings in the document.

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{CSSXRef(":heading_function", ":heading()")}}
125 changes: 125 additions & 0 deletions files/en-us/web/css/_colon_heading_function/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
title: :heading()
slug: Web/CSS/:heading_function
page-type: css-pseudo-class
browser-compat: css.selectors.headingfunction
sidebar: cssref
---

The **`:heading()`** [CSS](/en-US/docs/Web/CSS) [pseudo-class](/en-US/docs/Web/CSS/Pseudo-classes) function represents all [heading elements](/en-US/docs/Web/HTML/Reference/Elements/Heading_Elements) that match a value calculated using the `An+B` notation. 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](/en-US/docs/Web/CSS/CSS_cascade/Specificity#how_is_specificity_calculated) as a class selector, that is, `0-1-0`. So `:heading()` would have a specificity of `0-1-0`, and `section:heading()` would have a specificity of `0-1-1`.

## Syntax

```css-nolint
:heading([ <An+B> [, <An+B>]* | even | odd ]) {
/* ... */
}
```

### Parameters

The `:heading()` pseudo-class function takes a comma-separated list of `An+B` expressions or keyword values that describe a pattern for matching heading elements.

#### Keyword values

- `odd`
- : Represents the heading elements whose numeric position is odd: `<h1>`, `<h3>`, and `<h5>`.
- `even`
- : Represents the heading elements whose numeric position is even: `<h2>`, `<h4>`, and `<h6>`.

#### Functional notation

- `<An+B>`
- : Represents the heading elements whose numeric position matches the pattern `An+B`, for every positive integer or zero value of `n`, where:
- `A` is an integer step size,
- `B` is an integer offset,
- `n` is all nonnegative integers, starting from 0.

It can be read as the `An+B`-th element of a list. The `A` and `B` must both have {{cssxref("&lt;integer&gt;")}} values.

## Usage notes

The `:heading()` functional pseudo-class matches only elements that are semantically recognized as headings. It does not match elements with a [`role="heading"`](/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/heading_role) attribute, nor does it respect the ['aria-level'](/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-level) ARIA attribute.

## Examples

### Using keyword parameters

In this example, the `odd` keyword matches headings with odd-numbered levels, which are `<h1>` and `<h3>`. The `even` keyword is used to target even-numbered heading levels, `<h2>` and `<h4>`.

```html
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
```

```css
:heading(odd) {
color: tomato;
}
:heading(even) {
color: slateblue;
}
```

{{EmbedLiveSample("Keyword_example", "", "215")}}

### Using the `An+B` notation

```html
<h1>Science</h1>
<h2>Physics</h2>
<h3>Atomic, molecular, and optical physics</h3>
<h4>Optical physics</h4>
<h5>X-Rays</h5>
<h6>Discovery</h6>
```

```css hidden
main {
display: flex;
justify-content: space-around;
}
```

```css
/* Targets headings <h3> and <h4> */
:heading(3, 4) {
font-weight: 100;
}
/* Targets headings in reverse starting from <h3> */
:heading(-n + 3) {
color: tomato;
}
/* Targets every third heading starting from <h1> */
:heading(3n + 1) {
font-style: italic;
}
/* Targets headings after level 5 */
:heading(n + 5) {
color: slateblue;
}

In this example:
- `:heading(3, 4)` matches the `<h3>` and `<h4>` elements
- `:heading(-n + 3)` matches heading elements in reverse, so `<h3>`, `<h2>`, and `<h1>`
- `:heading(3n + 1)` matches every third (`3n`) heading element starting from `<h1>`, so this would include `<h1>` and `<h4>`
- `:heading(n + 5)` matches heading elements starting from `<h5>` and will include `<h6>`

{{EmbedLiveSample("Functional_notation_example", "", "292")}}

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{CSSXRef(":heading")}}
6 changes: 6 additions & 0 deletions files/en-us/web/css/pseudo-classes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ These pseudo-classes relate to the core identity of elements.

- {{CSSxRef(":defined")}}
- : Matches any element that is defined.
- {{CSSxRef(":heading")}}
- : Matches any heading element (`<h1>`-`<h6>`).

## Element display state pseudo-classes

Expand Down Expand Up @@ -165,6 +167,8 @@ These pseudo-classes relate to the location of an element within the document tr
- : Matches an element that is the last of its siblings.
- {{CSSxRef(":only-child")}}
- : Matches an element that has no siblings. For example, a list item with no other list items in that list.
- {{CSSXRef(":heading_function", ":heading()")}}
- : Uses `An+B` notation to select heading elements (`<h1>`-`<h6>`).
- {{CSSxRef(":nth-of-type", ":nth-of-type()")}}
- : Uses `An+B` notation to select elements from a list of sibling elements that match a certain type from a list of sibling elements.
- {{CSSxRef(":nth-last-of-type", ":nth-last-of-type()")}}
Expand Down Expand Up @@ -308,6 +312,8 @@ H

- {{CSSxRef(":has-slotted")}}
- {{CSSxRef(":has", ":has()")}}
- {{CSSXRef(":heading")}}
- {{CSSXRef(":heading_function", ":heading()")}}
- {{CSSxRef(":host")}}
- {{CSSxRef(":host_function", ":host()")}}
- {{CSSxRef(":host-context", ":host-context()")}}
Expand Down