Educational guide to link, action, and focus selectors
Real states, context, and privacy
Learn what depends on history, input device, focus, or the URL.
Mental model
These selectors observe browser state and should not be simulated as persistent values.
LVHALink rule order
At equal specificity, declare :link, :visited, :hover, and :active in that order so the cascade preserves the expected state.
:focus-visibleFocus and input modality
:focus means focused; :focus-visible applies the browser heuristic for showing an indicator, especially during keyboard navigation.
:visitedHistory is not an API
:visited restricts properties and hides matches from JavaScript to prevent history inference.
Links
Compare links with href, unvisited targets, and private history state.
:any-link
Links with href regardless of history.
Specificity(0, 1, 0)
:any-link
Behavior
- It is the union of :link and :visited.
- In HTML it applies to a and area with href.
Examples
a:any-linkMatches: <a href="/docs">Docs</a>
<a>Placeholder</a>Common mistakes
- An a element without href is not a hyperlink.
- It does not reveal visited state.
:link
Links the browser considers unvisited.
Specificity(0, 1, 0)
:link
Behavior
- It is mutually exclusive with :visited.
- It participates in the normal cascade.
Examples
a:linkMatches: <a href="/new">New page</a>
<a>Placeholder</a>Common mistakes
- It does not mean every a element.
- The result depends on the browser profile.
:visited
Links recorded in private browsing history.
Specificity(0, 1, 0)
:visited
Behavior
- Only limited visual properties, mainly colors, may differ.
- APIs return values as if the link were unvisited.
Examples
a:visitedMatches: <a href="/seen">Seen page</a>
<a href="/new">New page</a>Common mistakes
- Do not try to count matches with JavaScript.
- Do not use it to convey essential information.
Actions and focus
Use pointer, press, and keyboard navigation on native controls.
:hover
Elements designated by a pointing device.
Specificity(0, 1, 0)
:hover
Behavior
- The state follows the real pointer.
- On touch it may be brief, absent, or sticky.
Examples
button:hoverMatches: <button>Save</button>Common mistakes
- Do not hide essential content until hover.
- It is not equivalent to focus.
:active
Elements while they are being activated.
Specificity(0, 1, 0)
:active
Behavior
- It usually lasts while a press is held.
- It can apply to buttons and links.
Examples
button:activeMatches: <button>Save</button>Common mistakes
- It does not mean the current page.
- Its cascade order matters.
:focus
The element that currently has focus.
Specificity(0, 1, 0)
:focus
Behavior
- Focus can come from pointer, keyboard, or script.
- Usually one document element has focus at a time.
Examples
input:focusMatches: <input aria-label="Search" />Common mistakes
- Do not remove the indicator without a visible replacement.
- It does not automatically select the container.
:focus-visible
A focused element when the browser decides an indicator is needed.
Specificity(0, 1, 0)
:focus-visible
Behavior
- Keyboard modality commonly shows the indicator.
- The heuristic belongs to the browser.
Examples
button:focus-visibleMatches: <button>Continue</button>Common mistakes
- It is not simply focus after Tab.
- Do not fake a state that contradicts the heuristic.
Support and compatibility
See the dedicated Baseline status for :focus-visible.
:focus-within
An element when it or a descendant has focus.
Specificity(0, 1, 0)
:focus-within
Behavior
- It can highlight a whole group.
- The definition includes descendants in shadow trees.
Examples
label:focus-withinMatches: <label>Search
<input />
</label>Common mistakes
- The subject is the container, not the control.
- It does not replace the control focus style.
Support and compatibility
See the dedicated Baseline status for :focus-within.
URL and scope
Connect the active fragment and the root from which a query is evaluated.
:target
The element whose id matches the URL fragment.
Specificity(0, 1, 0)
:target
Behavior
- It changes with the hash.
- There may be no target.
Examples
#details:target<a href="#details">Open</a>
Matches: <section id="details">Details</section>Common mistakes
- It is not a freely chosen CSS state.
- The fragment also affects navigation and scrolling.
:scope
The contextual root of a query or scoped rule.
Specificity(0, 1, 0)
:scope
Behavior
- In querySelector() it represents the queried element.
- It can require direct children of that root.
Examples
:scope > article<section><!-- query root -->
Matches: <article>Direct</article>
<div><article>Nested</article></div>
</section>Common mistakes
- It is not always equivalent to :root.
- Its subject depends on evaluation context.
Support and compatibility
See the Baseline status for :scope in selector APIs.