Référence des Sélecteurs CSS
Guide de référence complet des sélecteurs CSS avec exemples.
56 selectors
Basic
*Universal selector — selects all elements
* { margin: 0; }elementType selector — selects all elements of that type
p { color: red; }.classClass selector — selects elements with that class
.card { border: 1px; }#idID selector — selects the element with that ID
#header { height: 60px; }Attribute
[attr]Attribute selector — elements with the attribute
[disabled] { opacity: 0.5; }[attr=value]Attribute equals value
[type="text"] { width: 100%; }[attr~=value]Attribute contains word (space-separated)
[class~="highlight"] { background: yellow; }[attr|=value]Attribute starts with value followed by hyphen
[lang|="en"] { font-family: sans; }[attr^=value]Attribute starts with value
[href^="https"] { color: green; }[attr$=value]Attribute ends with value
[src$=".jpg"] { border: 0; }[attr*=value]Attribute contains value (substring)
[href*="example"] { color: blue; }[data-*]Custom data attribute selector
[data-theme="dark"] { background: #111; }[attr i]Case-insensitive attribute match
[type="text" i] { padding: 4px; }Combinators
A BDescendant combinator — B is inside A (any depth)
div p { margin: 0; }A > BChild combinator — B is direct child of A
ul > li { list-style: none; }A + BAdjacent sibling — B immediately follows A
h2 + p { margin-top: 0; }A ~ BGeneral sibling — B follows A (not necessarily adjacent)
h2 ~ p { color: gray; }Pseudo-classes
:hoverElement being hovered by mouse
a:hover { text-decoration: underline; }:focusElement that has focus
input:focus { border-color: blue; }:activeElement being activated (clicked)
button:active { transform: scale(0.95); }:visitedLink that has been visited
a:visited { color: purple; }:linkUnvisited link
a:link { color: blue; }:first-childFirst child of its parent
li:first-child { font-weight: bold; }:last-childLast child of its parent
li:last-child { border-bottom: none; }:nth-child(n)Nth child of its parent
tr:nth-child(even) { background: #f5f5f5; }:nth-last-child(n)Nth child counting from the end
li:nth-last-child(2) { margin-bottom: 0; }:first-of-typeFirst element of its type among siblings
p:first-of-type { font-size: 1.2em; }:last-of-typeLast element of its type among siblings
p:last-of-type { margin-bottom: 0; }:nth-of-type(n)Nth element of its type among siblings
p:nth-of-type(2) { color: red; }:not(selector)Elements that do NOT match the selector
input:not([type="submit"]) { border: 1px solid; }:emptyElement with no children
div:empty { display: none; }:checkedChecked input (checkbox/radio)
input:checked + label { font-weight: bold; }:disabledDisabled form element
input:disabled { background: #eee; }:enabledEnabled form element
input:enabled { background: white; }:requiredRequired form element
input:required { border-left: 3px solid red; }:optionalOptional form element (not required)
input:optional { border-left: 3px solid gray; }:rootRoot element (html)
:root { --primary: #333; }:targetElement whose ID matches the URL fragment
:target { background: yellow; }:is()Matches any of the selectors in the list
:is(h1, h2, h3) { line-height: 1.2; }:where()Like :is() but with zero specificity
:where(h1, h2, h3) { color: black; }:has()Parent containing a matching child
figure:has(img) { border: 1px solid; }Pseudo-elements
::beforeCreates a pseudo-element before the element
a::before { content: "→ "; }::afterCreates a pseudo-element after the element
a::after { content: " ↗"; }::first-letterFirst letter of a block element
p::first-letter { font-size: 2em; }::first-lineFirst line of a block element
p::first-line { font-weight: bold; }::selectionPortion selected by the user
::selection { background: yellow; }::placeholderPlaceholder text in input
input::placeholder { color: gray; }::markerMarker of a list item
li::marker { color: red; }Grouping
A, BGrouping — multiple selectors share the same rules
h1, h2, h3 { font-family: sans; }A.classElement with specific class
p.intro { font-size: 1.1em; }A#idElement with specific ID
div#main { max-width: 800px; }Specificity
!importantOverrides all other declarations (avoid when possible)
.hide { display: none !important; }style=""Inline style (specificity = 1,0,0,0)
<p style="color:red">#id selector1 ID selector (specificity = 0,1,0,0)
#nav { background: black; }.class :pseudo1 class/pseudo-class/attribute (specificity 0,0,1,0)
.active { color: blue; }element1 type selector (specificity = 0,0,0,1)
p { color: black; }