CSS Selectors Reference

Selector syntax, how specificity is scored, and the rules that decide which declaration wins.

By BoltQuickTools team · Updated November 8, 2026

Specificity

Specificity is scored as three numbers (a, b, c) and compared left to right. To check a selector quickly, paste it into the CSS Specificity Calculator.

a
IDs
#id
b
Classes, attributes, pseudo-classes
.class, [attr], :hover
c
Types and pseudo-elements
div, ::before

A higher column always wins regardless of count: #id (1,0,0) beats .a.b.c.d (0,4,0). *, combinators and :where() add nothing. An inline style="" attribute is not part of the score at all; it is a separate step that beats any selector.

SelectorScoreWhy
li(0,0,1)One type
ul li::marker(0,0,3)Two types, one pseudo-element
.nav a:hover(0,2,1)Class, pseudo-class, type
#main .card(1,1,0)ID, class
:is(#a, .b) p(1,0,1):is() takes its most specific argument, even if #a is not the one matching
:where(#a, .b) p(0,0,1):where() always counts zero
:not(.x, #y)(1,0,0):not() and :has() also take their most specific argument
li:nth-child(2 of .x)(0,2,1)The pseudo-class, plus its most specific "of" selector, plus the type

What the browser compares, in order

  1. Origin and !important. Author !important beats normal author styles, inline ones included. Among important declarations, user and browser styles beat author styles.
  2. Inline style attribute against selector-based rules.
  3. Cascade layers. Unlayered normal styles beat layered ones, whatever the specificity. For !important, the layer order flips.
  4. Specificity.
  5. Source order. The last declaration wins.

Basic and attribute selectors

SelectorMatchesSpecificity
*Any element(0,0,0)
elementBy tag name, such as div or h1(0,0,1)
.classBy class name(0,1,0)
#idBy id attribute(1,0,0)
[attr]Has the attribute(0,1,0)
[attr="value"]Exactly equals value(0,1,0)
[attr~="word"]Space-separated list contains word(0,1,0)
[attr|="lang"]Equals "lang" or starts with "lang-"(0,1,0)
[attr^="prefix"]Starts with prefix(0,1,0)
[attr$="suffix"]Ends with suffix(0,1,0)
[attr*="text"]Contains text(0,1,0)
[attr="value" i]Case-insensitive value match. s forces case-sensitive.(0,1,0)

Combinators and lists

SyntaxMatchesExample
A BB anywhere inside Aarticle p
A > BB as a direct child of Aul > li
A + BB immediately after A, same parenth2 + p
A ~ BB anywhere after A, same parenth2 ~ p
A, BEither selectorh1, h2

One invalid selector in a plain comma list throws away the whole rule. :is() and :where() take forgiving lists, which skip invalid entries. :not() and :has() do not.

State pseudo-classes

:hoverPointer is over the element. Can stick after a tap on touch screens.
:focusHas focus, however it got there, including by mouse click.
:focus-visibleHas focus and the browser judges a visible indicator useful. Keyboard focus matches. Clicking a button usually does not; clicking a text input usually does.
:focus-withinThe element or a descendant has focus.
:activeBeing pressed.
:link / :visitedUnvisited or visited links. For privacy, :visited can change only color-type properties.
:any-linkAny link, visited or not.
:targetElement whose id matches the URL fragment.
:checkedChecked checkbox, radio or selected option.
:indeterminateCheckbox in mixed state, unset radio group, or progress with no value.
:defaultThe default control in a group.
:disabled / :enabledForm control availability.
:required / :optionalWhether the control has the required attribute.
:valid / :invalidValidation state. Matches before the user has typed anything.
:user-valid / :user-invalidValidation state, only after the user has interacted with the control.
:in-range / :out-of-rangeValue against min and max.
:placeholder-shownInput is currently showing its placeholder. Handy for "empty field" styling.
:read-only / :read-writeWhether the user can edit the element.
:autofillInput filled in by the browser.
:fullscreenElement is in fullscreen mode.
:modalA dialog opened with showModal(), or fullscreen element.
:popover-openA popover that is currently showing.
:definedBuilt-in element, or custom element that has been registered.

Structural pseudo-classes

:rootThe document root, <html> in HTML.
:emptyNo child elements or text. Comments are allowed; whitespace counts as text.
:first-child / :last-childFirst or last among siblings.
:only-childHas no siblings.
:first-of-type / :last-of-typeFirst or last sibling of its tag name.
:only-of-typeNo siblings of the same tag.
:nth-child(An+B)Position among all siblings, counting from 1. Accepts odd and even.
:nth-last-child(An+B)The same, counting from the end.
:nth-of-type(An+B)Position among siblings with the same tag.
:nth-child(An+B of S)Position among siblings that match selector S.
An+BSelects
3Only the third
2n or even2nd, 4th, 6th...
2n+1 or odd1st, 3rd, 5th...
n+3The third onward
-n+3The first three

.item:nth-child(2) does not mean "the second .item". It means "an .item that is also the second child". Use :nth-child(2 of .item) for the former.

Functional pseudo-classes

:is(a, b)Matches any listed selector. Useful for shortening repeated groups.
:where(a, b)Same as :is() with zero specificity. Good for resets and library defaults that should be easy to override.
:not(a, b)Matches none of the listed selectors.
:has(rel)Relational selector. div:has(> img) is a div with an img child; h2:has(+ p) is an h2 directly followed by a p. Cannot be nested inside another :has().
:lang(en)Language of the element, inherited from ancestors' lang. Also matches en-GB.
:dir(rtl)Text direction of the element.

Pseudo-elements

The double colon is standard. Browsers also accept a single colon for the four oldest: :before, :after, :first-line and :first-letter.

::before / ::afterGenerated first or last child box. Needs a content value. Does not generally apply to replaced elements like img or input.
::first-letterFirst letter of a block.
::first-lineFirst rendered line of a block.
::selectionText the user has highlighted.
::placeholderPlaceholder text of an input.
::markerList bullet or number. Takes a limited set of properties.
::backdropLayer behind a fullscreen element, modal dialog or popover.
::file-selector-buttonThe button in <input type="file">.
::part(name)Element a shadow tree exposes with part="name".
::slotted(sel)Light DOM content placed in a slot, from inside the shadow tree.

Nesting and scope

&Parent selector in CSS nesting: .btn { &:hover { color: red; } }. Specificity is calculated as if the parent were wrapped in :is().
:scopeThe scoping root. In el.querySelectorAll(":scope > li") it means el.
@scope (A) to (B)An at-rule, not a selector. Limits styles to inside A and stops at B. Check browser support before depending on it.
:host / :host(sel)The shadow host, from inside its shadow tree.

Common patterns

Last updated