CSS3 Selectors in Depth: Combinators, Attribute Selectors, Pseudo-Classes, Pseudo-Elements, Specificity and the Cascade

HTML5 and CSS3 60 min min read Updated: Nov 17, 2026 Intermediate
CSS3 Selectors in Depth: Combinators, Attribute Selectors, Pseudo-Classes, Pseudo-Elements, Specificity and the Cascade
Intermediate Topic 2 of 3

Combinators

SelectorMeaningExample
A BAny B inside A (descendant)nav a
A > BB that is a direct child of Aul > li
A + BB right after A (next sibling)h2 + p
A ~ BAny B after A with the same parenth2 ~ p

Attribute Selectors

css a[href^="https"] { color: green; } /* starts with */ a[href$=".pdf"] { font-weight: bold; } /* ends with */ a[href*="shop"] { text-decoration: underline; } /* contains */ input[type="email"] { border-color: #0b5fff; } [disabled] { opacity: .5; }

Pseudo-Classes: States and Positions

css a:hover { color: #0b5fff; } a:focus-visible { outline: 3px solid #8ab4ff; } button:active { transform: scale(.98); } li:first-child { font-weight: bold; } li:last-child { border-bottom: 0; } tr:nth-child(even) { background: #f3f6fb; } li:nth-child(3n) { color: crimson; } /* every 3rd */ p:not(.note) { margin-bottom: 1rem; } input:checked + label { font-weight: bold; }
  • :hover pointer is over it
  • :focus-visible focused by keyboard
  • :nth-child(2n) every second child
  • :not(x) everything except x
  • :checked, :disabled, :required form states

Pseudo-Elements: Parts of an Element

css p::first-line { font-weight: bold; } p::first-letter { font-size: 2em; } .quote::before { content: "\201C"; color: #0b5fff; } .quote::after { content: "\201D"; color: #0b5fff; } ::selection { background: #ffe08a; }

::before and ::after add generated content. They need the content property.

Specificity: Who Wins?

When two rules style the same property of the same element, the browser looks at specificity.

Selector typeStrength
Inline styleStrongest
Id selector #xHigh
Class, attribute, pseudo-class .x, [a], :hoverMedium
Element, pseudo-element p, ::beforeLow
Universal *None
css p { color: black; } /* low */ .intro { color: blue; } /* medium: wins over p */ p.intro { color: green; } /* medium + low: wins over .intro */ #top { color: red; } /* high: wins over classes */

The Cascade

  1. Importance: !important rules win (avoid using it)
  2. Specificity: the stronger selector wins
  3. Order: if still equal, the later rule wins

Some properties like color and font-family are inherited by child elements. Others like border and margin are not.

Debug with DevTools Right click the element, choose Inspect and look at the Styles panel. Crossed out rules lost the cascade.

Practice

  1. Style only links to PDF files in a different color
  2. Make every second row of a list gray
  3. Add an arrow after external links with ::after

Conclusion

Combinators, attribute selectors, pseudo-classes and pseudo-elements let you target elements precisely. Specificity and order decide conflicts.

Quick Summary: Combinators; attribute selectors; pseudo-classes; pseudo-elements; specificity; cascade.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators