ā¤ Like
šŸ”– Save
šŸ”— Share
Eric Hu
Eric Hu

CSS Selectors

@thedevspaceio

šŸ”Ž Write Maintainable Styles Using CSS Selectors

If you've ever written a CSS rule that didn't apply, or worse, applied everywhere, you know the struggle.

Selectors aren't just about targeting elements. They're about architecture, maintainability, and performance.

Here's your complete guide to mastering CSS selectors, from the basics to the latest superpowers: selector functions.

āœ… Basic selectors āœ… Combinators āœ… Attribute selectors āœ… Pseudo-classes āœ… Pseudo-elements āœ… Grouping & shorthand āœ… Selector functions (:is, :not, :has)

#CSS #Selectors #WebDev #Frontend


Basic selectors

SelectorMeaningExample
*Universal selector* { ... }
EType selector`p { ... }
.classClass selector.btn { ... }
#idID selector#nav { ... }
[attr]Attribute presencea[target] { ... }

Combinators

SelectorRelationshipExample
A BDescendant. B inside Aarticle p { ... }
A > BDirect childul > li { ... }
A + BAdjacent sibling. B immediately after Ah2 + p { ... }
A ~ BGeneral sibling. B after Ah2 ~ p { ... }

Attribute selectors

SelectorMeaningExample
[attr]Has attributeimg[alt] { ... }
[attr=value]Exact matcha[rel="external"] { ... }
[attr~=value]Word contains[class~="lead"] { ... }
[attr^=value]Starts witha[href^="https"] { ... }
[attr$=value]Ends withimg[src$=".svg"] { ... }
[attr*=value]Contains substring[data-id*="post-"] { ... }

Pseudo-classes (state and structural)

SelectorMeaningExample
:hoverOn hoverbutton:hover { ... }
:focusElement has focusinput:focus { ... }
:activeActivated by usera:active { ... }
:first-childFirst child of parentli:first-child { ... }
:last-childLast child of parentli:last-child { ... }
:nth-child(n)Select by index (1-based)tr:nth-child(odd) { ... }
:nth-of-type(n)Index among typep:nth-of-type(2) { ... }
:not(selector)Negation:not(.muted) { ... }
:disabled, :checkedForm statesinput:checked + label { ... }

Pseudo-elements

SelectorMeaningExample
::beforeInsert content before element.btn::before { ... }
::afterInsert content after element.note::after { ... }
::first-letterFirst letter stylingp::first-letter { ... }
::first-lineFirst line stylingp::first-line { ... }
::selectionSelected text::selection { ... }

Grouping & shorthand

SelectorMeaning
A, BGroup selectors. Apply same rules to both
E:not(:first-child)Combine structural and negation

Selector functions

Common functional pseudo-classes and selector helpers.

FunctionMeaning / notes
:not(X)Select elements that do NOT match X.
:is(X)Matches if element matches any selector in list X.
:where(X)Like :is() but with zero specificity.
:has(X)Parent selector: matches an element if it contains a descendant matching X.
:matches()Legacy alias for :is(). Prefer :is().
:nth-child(an+b)Structural numeric selector. Supports formulas like 2n, odd, even, etc.
:nth-last-child(an+b)Like :nth-child() but counting from the end.
:nth-of-type(an+b)Index among siblings of the same element type.
:nth-last-of-type(an+b)Like :nth-of-type() counting from the end.
:lang(language)Matches elements with the given language (matches lang attribute).

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io