CSS Pseudo-Classes: :hover, :has(), :nth-child Explained

Style elements by state and position: :hover, :focus-visible, :nth-child, :not and :has(), each with a live example.

Related chapters

Keep learning

Overview

Chapter 03 · pseudo-classes A pseudo-class is a phantom class on the element you named :hover, :focus-visible, :first-child, and :invalid describe an element that is already in the HTML. The browser applies the class. The colon always belongs to the element it is written on.

Phantom class

A pseudo-class is one word after a single colon, and it can sit anywhere a class could. You can chain tests that can be true together: a:hover:focus-visible is the same condition as a:focus-visible:hover. A link is never both :link and :visited, so that chain matches nothing.

2. Highlight whoever passes the test

Pick a selector. The blue outline is every match in this little page. Then hover, press, and Tab to the live button. Those three styles are CSS only.

3. Count siblings, or count one tag

:nth-child counts every child. :nth-of-type counts one tag name. A heading in the first slot makes the paragraph fail p:first-child.

An+B, from zero

n is 0, 1, 2, 3…. Element counting starts at 1, so 3n matches 3, 6, 9 and skips the 0. even is 2n. odd is 2n+1. -n+5 is the first five. :nth-last-child() starts at the last sibling and walks backward. :nth-child(1):nth-last-child(1) is :only-child with a higher specificity. A negative offset is written 4n-2. The form 3n+-2 is invalid, and the browser drops the whole rule.

4. Form state is already on the control

Type in the required email and leave the optional one blank. Then try 3 and 12 in the grams field. The borders and the badges are the pseudo-classes matching live.

5. :has() starts from the element you wrote it on

The selector inside the parentheses is relative to that element. It can look at descendants or at siblings.

And, or, not

:not(li, p) excludes either tag. :is() and :where() group selectors, and their lists forgive one invalid argument. :is() takes the specificity of its most specific argument. :where() adds zero. The selectors chapter shows how that tie plays out. A pseudo-element cannot sit inside :has() or :not(). A broad :has() makes the browser look up the tree when script changes a class, so keep the relative selector tight.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.