defuss-shadcn / Primitives / heading-anchor
Heading AnchorATM
A quiet § (or #) permalink on any heading - hanging in front of it and shown on hover by default, or inline before or after its text. A copyable deep link with no JavaScript.
On this page (6)
§Sizes
data-size pins the glyph size explicitly; without it the glyph inherits the heading's own font size (recommended - it then tracks the heading automatically). The glyph always inherits the heading's font family - var(--font-sans), var(--font-serif) and var(--font-mono) all work.
§In front of the heading (default)
Hover the heading: the glyph hangs in the gutter, so the heading's text keeps its own start edge. It also shows on keyboard focus and when the section is the address's target; on a touch screen it sits in front of the text instead. Leave about 1.5em of room at the heading's start.
§With # instead of §
The glyph is the link's text - a hash reads as a permalink to readers who know it from GitHub and Markdown.
§Before the heading text
data-placement="before" puts the glyph inline in front of the text, always shown.
§CSS view file
/* -- Heading Anchor component ------------------------------- */@layer components { .heading-anchor { /* the room between the glyph and the heading text */ --heading-anchor-gap: 0.375rem; color: var(--muted-foreground); text-decoration: none; font-weight: 400; /* -- In front (the default): the glyph hangs in front of the heading, in the gutter - the heading's text keeps its own start edge - and shows while the heading is hovered, the link is focused or the section is the target */ display: inline-block; inline-size: 1em; margin-inline: calc(-1em - var(--heading-anchor-gap)) var(--heading-anchor-gap); text-align: center; opacity: 0; transition: opacity 150ms ease; &:focus-visible { opacity: 1; } /* -- Before / after the text: inline, always shown ----------- */ &[data-placement="before"] { display: inline; inline-size: auto; margin-inline: 0 var(--heading-anchor-gap); opacity: 1; } &[data-placement="after"] { display: inline; inline-size: auto; margin-inline: var(--heading-anchor-gap) 0; opacity: 1; } &[data-size="xs"] { font-size: 0.75rem; } &[data-size="sm"] { font-size: 0.875rem; } &[data-size="md"] { font-size: 1rem; } &[data-size="lg"] { font-size: 1.125rem; } &[data-size="xl"] { font-size: 1.25rem; } &:hover { color: var(--primary); text-decoration: underline; } } /* the heading it sits in is hovered, or is the target of the address */ :hover > .heading-anchor, :target > .heading-anchor { opacity: 1; } /* no hover on touch screens, and a narrow gutter: the glyph goes back in front of the text, always shown */ @media (hover: none) { .heading-anchor:not([data-placement="before"], [data-placement="after"]) { display: inline; inline-size: auto; margin-inline: 0 var(--heading-anchor-gap); opacity: 1; } } @media (prefers-reduced-motion: reduce) { .heading-anchor { transition: none; } } @media (prefers-contrast: more) { .heading-anchor { color: var(--foreground); } } @media (forced-colors: active) { .heading-anchor { color: LinkText; } }}Comments, ideas or improvements? Edit this page's source on GitHub