CSS Selectors
Table of Contents
Patterns that target HTML elements for styling. Selector choice determines both which elements are styled and the specificity of the rule — higher specificity wins when rules conflict.
Related: CSS Core Concepts CSS Box Model CSS Layout css Cascading Style Sheets
Basic Selectors
| Selector | Syntax | Targets | Specificity |
|---|---|---|---|
| Element | p |
All <p> elements |
(0,0,0,1) |
| Class | .button |
Elements with class"button"= |
(0,0,1,0) |
| ID | #header |
Element with id"header"= |
(0,1,0,0) |
| Universal | * |
All elements | (0,0,0,0) |
| Attribute | [type"text"]= |
Elements with matching attribute | (0,0,1,0) |
#+beginsrc css p { color: blue; } .card { padding: 16px; } #nav { position: sticky; }
{ box-sizing: border-box; }
* Attribute selectors * [type="text"] { border: 1px solid gray; } a[href^="https"] { color: green; } * href starts with https * a[href$=".pdf"] { color: red; } * href ends with .pdf * [class*="btn"] { cursor: pointer; } * class contains "btn" * #+endsrc
Combinators
/* Descendant — any level deep */ div p { margin: 0; } /* Child — direct children only */ ul > li { list-style: square; } /* Adjacent sibling — immediately following */ h2 + p { text-indent: 20px; } /* General sibling — any following sibling */ h2 ~ p { border-top: 1px dashed; }
Pseudo-classes
State-based selectors — no extra HTML required:
/* User interaction */ a:hover { text-decoration: underline; } a:visited { color: purple; } button:focus { outline: 2px solid blue; } button:active { transform: scale(0.98); } /* Form state */ input:disabled { opacity: 0.5; } input:checked + label { font-weight: bold; } /* Structural */ li:first-child { font-weight: bold; } li:last-child { border-bottom: none; } li:nth-child(odd) { background: #f5f5f5; } li:nth-child(3n) { color: red; } /* every 3rd */ p:empty { display: none; } :root { --primary: #3498db; } /* CSS custom properties */ /* Negation */ button:not(.disabled) { cursor: pointer; } li:not(:last-child) { border-bottom: 1px solid; }
Pseudo-elements
Style a part of an element — always double colon (::):
/* Insert content */ .required::after { content: " *"; color: red; } .quote::before { content: "\201C"; /* " */ } /* Text selection highlight */ ::selection { background: #3498db; color: white; } /* Placeholder text */ input::placeholder { color: #aaa; } /* First line / first letter */ p::first-line { font-variant: small-caps; } p::first-letter { font-size: 2em; float: left; }
Grouping
/* Same styles for multiple selectors */ h1, h2, h3, h4 { font-family: sans-serif; } .card, .panel, .modal { border-radius: 8px; }
Specificity
When rules conflict, specificity determines the winner.
Calculated as (ID, class/attr/pseudo-class, element/pseudo-element):
p /* (0,0,1) */ .card /* (0,1,0) */ #nav /* (1,0,0) */ #nav .item p /* (1,1,1) */
Higher specificity always wins regardless of source order.
!important overrides all specificity — use sparingly.
Tips
- Prefer class selectors over IDs for styling — IDs are very high specificity and make overriding difficult
- Keep selector chains short —
.nav .item ais fine;header nav ul li a spanis a maintenance problem :nth-childis powerful for tables and lists —tr:nth-child(even)gives alternating row colours without JavaScript::beforeand::afterrequirecontent: ""to render — even if the content is empty