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.
#id.class, [attr], :hoverdiv, ::beforeA 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.
| Selector | Score | Why |
|---|---|---|
| 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
- Origin and
!important. Author!importantbeats normal author styles, inline ones included. Among important declarations, user and browser styles beat author styles. - Inline style attribute against selector-based rules.
- Cascade layers. Unlayered normal styles beat layered ones, whatever the specificity. For
!important, the layer order flips. - Specificity.
- Source order. The last declaration wins.
Basic and attribute selectors
| Selector | Matches | Specificity |
|---|---|---|
| * | Any element | (0,0,0) |
| element | By tag name, such as div or h1 | (0,0,1) |
| .class | By class name | (0,1,0) |
| #id | By 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
| Syntax | Matches | Example |
|---|---|---|
| A B | B anywhere inside A | article p |
| A > B | B as a direct child of A | ul > li |
| A + B | B immediately after A, same parent | h2 + p |
| A ~ B | B anywhere after A, same parent | h2 ~ p |
| A, B | Either selector | h1, 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
| :hover | Pointer is over the element. Can stick after a tap on touch screens. |
| :focus | Has focus, however it got there, including by mouse click. |
| :focus-visible | Has 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-within | The element or a descendant has focus. |
| :active | Being pressed. |
| :link / :visited | Unvisited or visited links. For privacy, :visited can change only color-type properties. |
| :any-link | Any link, visited or not. |
| :target | Element whose id matches the URL fragment. |
| :checked | Checked checkbox, radio or selected option. |
| :indeterminate | Checkbox in mixed state, unset radio group, or progress with no value. |
| :default | The default control in a group. |
| :disabled / :enabled | Form control availability. |
| :required / :optional | Whether the control has the required attribute. |
| :valid / :invalid | Validation state. Matches before the user has typed anything. |
| :user-valid / :user-invalid | Validation state, only after the user has interacted with the control. |
| :in-range / :out-of-range | Value against min and max. |
| :placeholder-shown | Input is currently showing its placeholder. Handy for "empty field" styling. |
| :read-only / :read-write | Whether the user can edit the element. |
| :autofill | Input filled in by the browser. |
| :fullscreen | Element is in fullscreen mode. |
| :modal | A dialog opened with showModal(), or fullscreen element. |
| :popover-open | A popover that is currently showing. |
| :defined | Built-in element, or custom element that has been registered. |
Structural pseudo-classes
| :root | The document root, <html> in HTML. |
| :empty | No child elements or text. Comments are allowed; whitespace counts as text. |
| :first-child / :last-child | First or last among siblings. |
| :only-child | Has no siblings. |
| :first-of-type / :last-of-type | First or last sibling of its tag name. |
| :only-of-type | No 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+B | Selects |
|---|---|
| 3 | Only the third |
| 2n or even | 2nd, 4th, 6th... |
| 2n+1 or odd | 1st, 3rd, 5th... |
| n+3 | The third onward |
| -n+3 | The 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 / ::after | Generated first or last child box. Needs a content value. Does not generally apply to replaced elements like img or input. |
| ::first-letter | First letter of a block. |
| ::first-line | First rendered line of a block. |
| ::selection | Text the user has highlighted. |
| ::placeholder | Placeholder text of an input. |
| ::marker | List bullet or number. Takes a limited set of properties. |
| ::backdrop | Layer behind a fullscreen element, modal dialog or popover. |
| ::file-selector-button | The 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(). |
| :scope | The 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
- Zebra-striped rows:
tr:nth-child(even) { background: #f5f5f5; } - External links:
a[href^="http"]:not([href*="example.com"]) - PDF links:
a[href$=".pdf"]::after { content: " (PDF)"; } - First paragraph of an article:
article > p:first-of-type - Cards that contain an image:
.card:has(img) - Page-wide state without JavaScript:
body:has(dialog[open]) { overflow: hidden; } - Spacing between items only:
.stack > * + * { margin-top: 1rem; } - Low-specificity reset:
:where(h1, h2, h3) { margin: 0; } - Focus rings for keyboard users:
:focus-visible { outline: 2px solid blue; } - Show errors only after interaction:
input:user-invalid { border-color: red; }
Last updated