Zach’s ugly mug (his face) Zach Leat­herman

<heading-anchors> Web Component

Looking out to sea over Peel Beach and Peel Castle, a rusted boat anchor sits on grass
August 29, 2024
🚾Web Component@zachleat/heading-anchors

Adds and positions sibling anchor links for heading elements (h1,h2,h3,h4,h5,h6) when they have an `id` attribute.

All🚾 0.1%
Popularity
npm51k Monthly downloads sparkline for @zachleat/heading-anchors, ranging 3k to 8k cdn1k Monthly jsDelivr CDN hits sparkline for @zachleat/heading-anchors, ranging 6 to 905
Versions
6 versions in
1.0.5
Audit
0 reports
0 deps
Issues
2
4
PRs
0

<heading-anchors> adds sibling anchor links for heading elements (h1–h6) when they have an id attribute and positions those anchor links visually as though they were nested inside of the heading.

Inspired by David Darnes’ component of the same name.

Currently in use on v9 of the eleventy-base-blog project (and on this very web site, too).

Features

  • Useful when you want preserve heading text as is (selectable, able to nest other links, etc).
  • Useful when you want to exclude anchor links from your RSS feed.
  • Links are positioned alongside heading text, but not nested inside of heading markup (for improved screen-reader compatibility and accessibility)
  • Establishes a placeholder element to establish space for the anchor link and so that heading text doesn’t reflow during interaction
  • Prefers the CSS Anchoring API (where available) but works using JavaScript positioning when the API is not available.
  • Automatically matches font styling of the heading (font-family, line-height, font-size, and font-weight)

Prior Art

IndieWeb Avatar for https://unsplash.com/Poster image by James Qualtrough

Zach Leatherman is a builder for the web at Font Awesome and the creator of Build Awesome (née Eleventy/11ty), an award-winning open source website generator. He measures website performance with speedlify and at one point became too fixated on web fonts. He has given 86 talks in nine different countries at events like Beyond Tellerrand, Smashing Conference, Jamstack Conf, CSSConf, and The White House. Learn more about Zach »