CSS ::before and ::after Pseudo-Elements Explained

::before, ::after, ::marker, ::placeholder and ::selection with live examples, and why ::before needs content to appear.

Related chapters

Keep learning

Overview

Chapter 04 · pseudo-elements A pseudo-element is a box the HTML never wrote ::before and ::after insert fictional boxes as the first and last children. A pseudo-class, from the previous chapter, only describes an element that is already there. Pseudo-elements use two colons. The old single-colon spelling still parses for the original four: :before, :after, :first-line, and :first-letter.

1. content creates the box

Turn the pieces on. Then turn off content: "" and watch the gold line vanish even though height and background are still written.

The set

::before and ::after need content. An empty string still counts. none and normal generate nothing. ::marker is the bullet or number. ::placeholder is the hint in an empty field, and a real value replaces it. ::file-selector-button is only the button on a file input. ::selection paints highlighted text, and it accepts color, background, text decoration, and text shadow. One pseudo-element ends the selector. Pseudo-classes come first: a:hover::before.

2. The first line is whatever currently fits

Drag the width. The purple run is the first displayed line, so it changes when the column changes. Then switch the paragraph to inline.

Block containers

::first-letter and ::first-line apply to block containers such as headings and paragraphs. The first letter includes punctuation that sits against it, and the browser decides that boundary. ::first-line can set font, color, background, and similar text styles. ::first-letter can also take margin, padding, border, and float, which is how a drop cap sits beside the line. If the first line splits a link or an em, the first-line styles cover only the part that sits on that line.

3. The fictional box can copy an attribute

The paragraph’s words come from React. The mark in front of them comes from content. The image beside it is given the same ::before rule. The check mark and the kind label are decoration, so they can live in CSS. attr() only copies an attribute that is already on the element React rendered. Words a person must read belong in the children, because a pseudo-element is easy to miss in a screen reader and impossible to pass as a prop. img, input, br, and hr are replaced elements: a ::before rule on them does not paint a box.

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.