jQuery :header Selector

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
jQuery extension · jQuery 1.2+

What You’ll Learn

The :header selector matches every heading element — h1, h2, h3, h4, h5, and h6 — in one jQuery expression. jQuery extension since 1.2; official docs recommend scoping with CSS first, then .filter(":header") for performance.

01

Syntax

:header

02

Tags

h1–h6 only

03

Official

Page styling

04

.filter()

Performance

05

Scope

article :header

06

CSS list

h1, h2, …

Introduction

Pages often mix six heading levels. When you want to style every heading at once — add a background, unify color, build a table of contents, or attach click handlers — listing h1, h2, h3, h4, h5, h6 every time is repetitive. jQuery’s :header pseudo-class selects all of them in a single expression.

jQuery has supported :header since version 1.2. Official documentation describes it as selecting all elements that are headers, like h1, h2, h3, and so on. Because it is a jQuery extension, prefer $("h1, h2, h3, h4, h5, h6") or $("article *").filter(":header") when you need native selector speed on large documents.

Understanding the :header Selector

Think of :header as a shortcut for “any heading tag”:

  • <h1>Title</h1> + :header → matches.
  • <h3>Section</h3> + :header → matches.
  • <p>Not a heading</p> + :header → no match.
  • <div class="title">…</div> styled like a title → no match (not an h1–h6 tag).
💡
Beginner Tip

Do not confuse :header with HTML <header> landmark elements or HTTP headers. This selector only targets h1 through h6 tags.

📝 Syntax

Official jQuery API form (since 1.2):

jQuery
jQuery( ":header" )

// typical usage:
$( ":header" )
$( "article :header" )
$( "#sidebar :header" )

// performance pattern — CSS scope, then filter:
$( "article *" ).filter( ":header" )

// CSS-equivalent (native querySelectorAll):
$( "h1, h2, h3, h4, h5, h6" )

Parameters

  • None:header takes no arguments. It always means h1, h2, h3, h4, h5, or h6.

Return value

  • A jQuery object containing every matched heading element in document order.
  • Empty collection when no headings exist in the search scope.

Official jQuery API example

jQuery
$( ":header" ).css({
  background: "#ccc",
  color: "blue"
});

⚡ Quick Reference

Goal:headerCSS equivalent
All headings on page$(":header")h1, h2, h3, h4, h5, h6
Headings in article$("article :header")article h1, … h6
Performance pattern$("article *").filter(":header")Scope then filter
Only h2 levelUse h2 tagNot :header
Native CSSjQuery extensionStandard tag list

📋 :header vs tag list, <header>, and other selectors

Heading tags vs landmark elements vs text filters.

:header
$(":header")

h1–h6 tags

Tag list
h1, h2, …, h6

Native CSS

<header>
$("header")

Landmark tag

:contains
h2:contains("Intro")

Text substring

Examples Gallery

Example 1 follows the official jQuery :header demo. Examples 2–5 cover the .filter(":header") pattern, scoped headings, CSS-equivalent lists, and a table-of-contents helper.

📚 Official jQuery Demo

Style every heading with gray background and blue text.

Example 1 — Official Demo: $(":header")

Official jQuery demo — all h1–h6 elements get background #ccc and blue text; paragraphs stay unchanged.

jQuery
$( ":header" ).css({
  background: "#ccc",
  color: "blue"
});
Try It Yourself

How It Works

jQuery tests each candidate element’s tag name. Only h1, h2, h3, h4, h5, and h6 pass the :header filter.

Example 2 — Performance: .filter(":header")

Official performance guidance — scope with a CSS selector, then filter to headings only.

jQuery
// instead of scanning the whole document:
// $( ":header" ).css({ … });

// recommended when you know the region:
$( "#content *" ).filter( ":header" ).css({
  background: "#ccc",
  color: "blue"
});
Try It Yourself

How It Works

.filter(":header") keeps only heading tags from the scoped collection — same result as $("#content :header") with a flexible two-step pattern.

📈 Practical Patterns

Scoped selection, native CSS lists, navigation helpers.

Example 3 — Scoped Headings: article :header

Style headings inside articles only — skip sidebar or footer headings.

jQuery
$( "article :header" ).css( "borderLeft", "4px solid #2563eb" );
Try It Yourself

How It Works

The ancestor selector limits where :header runs — only headings inside article elements match.

Example 4 — CSS Equivalent: h1, h2, …, h6

Native CSS tag list produces the same matches — useful for querySelectorAll and modern code.

jQuery
var viaHeader = $( ":header" ).length;
var viaTags = $( "h1, h2, h3, h4, h5, h6" ).length;

console.log( ":header →", viaHeader );
console.log( "tag list →", viaTags );
Try It Yourself

How It Works

:header is shorthand. When you need native selector performance, the explicit tag list is the standard CSS approach.

Example 5 — Table of Contents from :header

Build a clickable outline by iterating every heading on the page.

jQuery
var $list = $( "<ul id='toc'></ul>" );

$( ":header" ).each(function ( i ) {
  var id = "section-" + i;
  $( this ).attr( "id", id );
  $list.append(
    $( "<li><a></a></li>" )
      .find( "a" )
      .attr( "href", "#" + id )
      .text( $( this ).text() )
      .end()
  );
});

$( "body" ).prepend( $list );
Try It Yourself

How It Works

:header gathers every heading in one pass — ideal for TOC plugins, anchor IDs, and outline navigation.

🚀 Common Use Cases

  • Global styling — unify all headings with $(":header").css(...) as in the official demo.
  • Documentation — build table-of-contents links from every heading.
  • CMS themes — add anchor IDs to h1–h6 in article bodies only.
  • Accessibility audits — count headings with $(":header").length.
  • Print styles — add page-break rules to all heading levels at once.
  • Performance — scope with CSS, then .filter(":header") on large pages.

🧠 How jQuery Evaluates :header

1

Build candidate set

Evaluate any prefix — e.g. all elements, or descendants of article.

Query
2

Test tag name

Keep elements whose node name is h1, h2, h3, h4, h5, or h6.

Filter
3

Preserve order

Return headings in document order — top to bottom as they appear in the DOM.

Sort
4

Return collection

Chain .css(), .each(), or .on() on every heading at once.

📝 Notes

  • Available since jQuery 1.2 — jQuery extension, not native CSS.
  • Matches h1, h2, h3, h4, h5, and h6 only — not <header> landmark elements.
  • CSS equivalent: h1, h2, h3, h4, h5, h6.
  • Prefer $("region *").filter(":header") over bare $(":header") on large documents per official docs.
  • Combine with descendant scope — article :header, #main :header.
  • Not related to HTTP headers or jQuery .header() (no such method).

Browser Support

The :header pseudo-class is a jQuery extension and works in jQuery 1.2+. It runs through Sizzle/jQuery’s selector engine, so behavior is consistent wherever jQuery runs. Use h1, h2, h3, h4, h5, h6 when you need native querySelectorAll without jQuery.

jQuery 1.2+ · extension

jQuery :header Selector

Use h1–h6 tag lists for native CSS; use :header for concise jQuery collections.

100% jQuery only
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:header Extension

Bottom line: Scope with CSS first, then .filter(":header") on large pages.

Conclusion

The :header selector matches every heading element — h1 through h6 — in one expression. The official demo applies background and text color to all headings on the page.

Use it for global heading styles and table-of-contents helpers, scope it with container selectors, and prefer .filter(":header") after a CSS scope when performance matters.

💡 Best Practices

✅ Do

  • Use $("article :header") to limit scope
  • Prefer .filter(":header") after a CSS region selector
  • Use h1, …, h6 when you need native querySelectorAll
  • Combine with .each() for TOC and anchor generation
  • Pick specific levels (h2) when you only need one

❌ Don’t

  • Confuse :header with <header> landmark tags
  • Expect styled divs to match — only h1–h6 tags count
  • Run bare $(":header") on huge DOM trees unnecessarily
  • Use :header in native CSS stylesheets — it is jQuery-only
  • Confuse with :contains() or :has() — different filters

Key Takeaways

Knowledge Unlocked

Five things to remember about :header

All heading tags in one selector.

5
Core concepts
6 02

Levels

All six

Rule
.filter 03

Performance

Scope first

Tip
demo 04

Official

.css() all

Demo
h1…h6 05

CSS list

Native alt

Compare

❓ Frequently Asked Questions

:header selects every heading element in the matched set — h1, h2, h3, h4, h5, and h6. One pseudo-class replaces writing h1, h2, h3, h4, h5, h6 when you need all heading levels at once. Available since jQuery 1.2.
No. :header only matches actual h1–h6 tags. A div with class "title" or a p with bold text is not a header unless it uses a heading tag.
No. It is a jQuery extension and does not work in native querySelectorAll(). The CSS-equivalent selector is h1, h2, h3, h4, h5, h6.
Official jQuery docs recommend selecting with a pure CSS selector first, then narrowing with .filter(":header"). Example: $("article *").filter(":header") instead of bare $(":header") on huge pages when you only care about one region.
Yes. Prefix with a container — $("article :header") matches only headings inside article elements. $("section.main :header") adds a class filter too.
:header matches element type (heading tags). :contains() matches visible text substrings. :has() matches parents that contain a descendant. They solve different problems.
Did you know?

HTML5 introduced the <header> landmark element for page sections, but jQuery’s :header predates it and still only matches h1h6 tags. To style a landmark region, use $("header") — the element selector — instead.

Continue to :hidden Selector

After matching heading tags with :header, learn how :hidden finds elements that consume no layout space.

:hidden selector tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful