<htmlstart/>

CSS Selectors

Selectors target which HTML elements to style.

Basic Selectors

/* Element — all h1 tags */
h1 { color: red; }

/* Class — elements with class="card" */
.card { padding: 16px; }

/* ID — element with id="header" */
#header { background: black; }

/* Universal — every element */
* { box-sizing: border-box; }

/* Attribute */
input[type="email"] { border-color: blue; }

Combinators

SelectorMeaningExample
SpaceDescendant (any level deep)nav a
>Direct child onlyul > li
+Adjacent siblingh2 + p
~All siblings afterh2 ~ p
,Group — apply to bothh1, h2

Pseudo-classes & Pseudo-elements

a:hover   { color: red; }          /* mouse over */
li:first-child { font-weight: bold; }
input:focus { outline: 2px solid blue; }

p::first-line { font-weight: bold; } /* first line of text */
li::marker    { color: red; }        /* bullet/number */
Prefer classes over IDs for styling — classes are reusable, IDs are not.
🧩

Test Yourself

4 questions
Q1.What prefix targets elements by class name?
Q2.What prefix targets an element by its ID?
Q3.Which combinator selects ONLY direct children?
Q4.Which pseudo-class applies styles when hovering with the mouse?