Chapter 4 of 17 · CSS for Beginners — Complete Free Tutorial

Combinators & Pseudo-classes

Real pages need selectors like "links inside the sidebar" or "every second row". Combinators connect elements by relationship, and pseudo-classes select by state — together they cover most day-to-day styling needs.

Combinators: selecting by relationship

CombinatorExampleMeaning
Descendant (space).nav aAny <a> inside .nav, at any depth
Child (>).nav > liOnly direct children
Adjacent sibling (+)h2 + pThe element immediately after
General sibling (~)h2 ~ pEvery sibling after it
.sidebar a        { color: #334155; }   /* all links in the sidebar */
.menu > li        { display: inline; }  /* only top-level items */
h2 + p            { margin-top: 0.5rem; } /* pull the intro closer */
.error input      { border-color: red; }

Expected output: in an erroring form, every input inside the .error container gets a red border — a whole-state style from one rule.

Pseudo-classes: selecting by state

A pseudo-class is a keyword after a colon that describes a condition rather than an attribute.

Interaction states

a:hover        { color: #4338ca; }
button:active  { transform: scale(0.97); }
a:focus-visible {
  outline: 2px solid #4f46e5;
  outline-offset: 3px;
}

Prefer :focus-visible over :focus for outlines — it shows the ring for keyboard users but not after mouse clicks, exactly what you want. Never remove outlines without a replacement (accessibility).

Structural pseudo-classes

li:first-child  { font-weight: 700; }
li:last-child   { border-bottom: 0; }
tr:nth-child(even) { background: #f8fafc; }   /* zebra stripes */
li:nth-child(3) { color: crimson; }           /* the third item */
li:not(.muted)  { opacity: 1; }

Expected output: a table body with alternating row colours — no extra classes in the HTML at all.

Form pseudo-classes

input:required   { border-left: 3px solid #f59e0b; }
input:disabled   { background: #e2e8f0; color: #94a3b8; }
input:checked    { accent-color: #4f46e5; }

Practical example: a styled navigation

<nav class="menu">
  <ul>
    <li><a href="/">Home</a></li>
    <li class="active"><a href="/tutorials">Tutorials</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>
.menu ul         { display: flex; gap: 1.25rem; list-style: none; }
.menu a          { text-decoration: none; color: #334155; }
.menu li.active a,
.menu a:hover    { color: #4f46e5; }
.menu li + li::before { content: "·"; margin-right: 1.25rem; color: #94a3b8; }

The li + li::before rule adds a dot separator only between items (it uses the adjacent-sibling combinator plus a pseudo-element — the latter is next chapter). Expected output: a horizontal menu whose active and hovered items turn indigo.

Common mistakes

  • Descendant where child was meant — .card a also hits links in nested cards. Use > when depth matters.
  • Removing focus styles — outline: none without :focus-visible replacement locks keyboard users out.
  • Over-specific selectors — .sidebar ul li a.icon svg is fragile; two descendant steps are usually enough.
  • Using :hover for essential actions — phones have no hover; keep primary actions visible at all times.

Practice checklist

  1. Add zebra striping to any table with tbody tr:nth-child(even).
  2. Style a menu so the current page uses an .active class AND hover works — via one grouped selector.
  3. Build an error state: give a container class .error and style every input inside with a single descendant selector.

Next: styles that exist without any element at all — Pseudo-elements & Attribute Selectors.

Quiz: Combinators & Pseudo-classes

5 questions · 70% to pass

Frequently Asked Questions

CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.

The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.

Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.

No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.

Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.

Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.

Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.

Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.

Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.

JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.