Pseudo-Elements in Practice: Decorative Details Without Extra Markup

Practical CSS pseudo-elements: ::before and ::after recipes, stretched links, counters, ::marker, ::placeholder, ::backdrop, and accessibility rules.

Executive Summary: Pseudo-elements let CSS style parts of an element that have no tag of their own, or generate new boxes with ::before and ::after. Used well, they remove empty decorative spans, make whole cards clickable with a single link, number steps automatically and style list markers, placeholders, dialog backdrops and file inputs. The rules that keep them safe: decoration only, never essential content; give generated symbols empty alt text; and remember they cannot attach to images, inputs or other replaced elements.
Content box flanked by generated before and after pseudo-element boxes

Inspect almost any marketing site and you will find markup like <span class="divider"></span>, <i class="arrow"></i> and <div class="overlay"></div>. None of these carry meaning. They exist because someone needed a box to paint on. Each one is noise for screen readers, a hook for layout bugs, and something every template has to remember to include.

A pseudo-element is a selector that targets a part of an element, or a box the browser generates, rather than an element in the DOM. They use a double colon: ::before, ::after, ::marker, ::placeholder. The most used pair, ::before and ::after, create a child box at the very start or end of the element’s content, as long as you give them a content value. For the difference between pseudo-elements and pseudo-classes, see my post on pseudo-elements and pseudo-classes; this guide is about using them in production.

My position: if a box exists only to be painted, it belongs in CSS. If it carries information a user needs, it belongs in HTML. Pseudo-elements are excellent at the first job and dangerous at the second.

How ::before and ::after work

  • They are inserted inside the element, as its first and last child. A ::before on a button sits inside the button, before its text.
  • They do not exist until content is set. content: "" is enough for a purely visual box.
  • They are inline by default. Set display: block, or position them absolutely, to give them dimensions.
  • They count as a type selector for specificity, (0, 0, 1).
  • Each element gets at most one of each.
  • They do not work on replaced elements: img, video, iframe, and most form inputs have no content box to insert into. Wrap the element or use a parent.

What content can hold

.a::before { content: ""; }                    /* empty box for decoration */
.b::before { content: "New"; }                 /* a string */
.c::after  { content: attr(data-count); }      /* an attribute value, as text */
.d::before { content: counter(step) ". "; }    /* a counter */
.e::before { content: url("/icons/star.svg"); } /* an image */
blockquote::before { content: open-quote; }    /* locale-aware quotation marks */
.f::after  { content: "→" / ""; }             /* a symbol with empty alternative text */

The last line uses the alternative-text syntax: anything after the slash replaces the content for assistive technology. It is supported in all current browsers and it fixes a real problem, covered next.

Accessibility rules for generated content

Screen readers generally announce text generated with content. That surprises people who assume CSS is invisible to assistive technology. It also cuts both ways:

  • Decorative symbols get announced. An arrow or emoji added with content: "→" may be read as “right arrow”. Give purely decorative content an empty alternative: content: "→" / "".
  • Essential information is fragile in CSS. Generated text cannot be selected or copied, often is not translated by browser translation tools, and disappears entirely if the stylesheet fails to load. Prices, labels, status text and instructions belong in HTML.
  • Meaningful symbols need real words. If an icon means something, give the alternative text real words: content: "↗" / "external link".

Seven recipes worth keeping

.card { position: relative; }

.card__title a::after {
  content: "";
  position: absolute;
  inset: 0;               /* cover the whole card */
}

.card :is(button, .card__secondary-link) {
  position: relative;
  z-index: 1;             /* keep other controls clickable above the overlay */
}

The card has exactly one link, in its heading, so screen reader users hear a short, clear link name instead of the entire card’s text. Sighted users can click anywhere on the card. This beats wrapping the whole card in an <a>, which makes the link name enormous and breaks any nested buttons.

2. Accent bars and decorative lines

.section-title {
  position: relative;
  padding-block-end: 0.5rem;
}

.section-title::after {
  content: "";
  position: absolute;
  inset-inline-start: 0;
  inset-block-end: 0;
  inline-size: 3rem;
  block-size: 3px;
  background: var(--color-accent);
  border-radius: 2px;
}

Logical properties keep the bar on the start side in right-to-left layouts, as described in the logical properties guide.

3. A legibility overlay on hero images

.hero {
  position: relative;
  isolation: isolate;     /* keep the negative z-index inside the hero */
}

.hero::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: linear-gradient(to top, oklch(15% 0.02 255 / 0.8), transparent 60%);
}

The isolation: isolate line matters. Without it, z-index: -1 can push the overlay behind the hero’s own background, or behind the page. The stacking context rules are in the box model and positioning guide.

4. Automatic step numbers with counters

.steps {
  counter-reset: step;
  list-style: none;
  padding: 0;
}

.steps > li {
  counter-increment: step;
  display: grid;
  grid-template-columns: 2.5rem minmax(0, 1fr);
  gap: 1rem;
}

.steps > li::before {
  content: counter(step);
  display: grid;
  place-items: center;
  inline-size: 2.5rem;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--color-accent);
  color: white;
  font-weight: 600;
}

Steps renumber themselves when content editors add, remove or reorder items. Because the list is still an ol or ul in HTML, assistive technology still announces it as a list. If the order matters, use ol, since removing the default markers with list-style: none can cause some browsers, notably Safari, to stop announcing the list semantics. Adding role="list" restores them.

5. Styling list markers with ::marker

.checklist li::marker {
  content: "✓  ";
  color: var(--color-success);
}

ol.fancy li::marker {
  color: var(--color-accent);
  font-weight: 700;
}

::marker styles the real bullet or number of a list item, so you no longer need to hide markers and recreate them with ::before. It supports a limited set of properties, mainly color, font properties, content and a few text properties, so use the counter recipe when you need backgrounds or shapes.

6. Quotation marks that follow the language

blockquote { quotes: auto; }
blockquote::before { content: open-quote; }
blockquote::after  { content: close-quote; }

With quotes: auto, the browser picks marks based on the element’s lang: curly double quotes for English, guillemets for French, low-high quotes for German.

7. Drop caps with ::first-letter

.article > p:first-of-type::first-letter {
  float: inline-start;
  font-size: 3.2em;
  line-height: 0.9;
  margin-inline-end: 0.08em;
  font-weight: 700;
}

The initial-letter property produces better-aligned drop caps where it is supported; use the float version as the baseline.

The other pseudo-elements you should know

Pseudo-element Targets Practical use
::placeholder Placeholder text in inputs Lighter color, but keep 4.5:1 contrast if it carries hints
::selection Text the user has selected Brand-colored selection with readable text
::backdrop The layer behind a modal dialog or popover Dimmed or blurred background for modals
::file-selector-button The button in input type="file" Matching your button styles without hiding the input
::details-content The collapsible part of <details> Animating open and close; cross-browser since 2025
::first-line The first rendered line of a block Small caps lead-ins in editorial layouts
::highlight(name) Ranges registered with the Custom Highlight API Search-term highlighting without wrapping text in spans
::view-transition-* Snapshots during a view transition Customizing transitions, see the View Transitions guide
dialog::backdrop {
  background: oklch(15% 0.02 255 / 0.55);
  backdrop-filter: blur(4px);
}

input[type="file"]::file-selector-button {
  font: inherit;
  padding: 0.5rem 1rem;
  border: 0;
  border-radius: var(--radius-control);
  background: var(--color-accent);
  color: white;
  margin-inline-end: 0.75rem;
}

::selection {
  background: oklch(85% 0.1 255);
  color: oklch(20% 0.02 255);
}

Styling ::file-selector-button directly is much better than the old trick of hiding the file input and overlaying a fake button, which often broke keyboard access and the announced file name.

Frequently Asked Questions

What is the difference between ::before and ::after?

Both generate a box inside an element when given a content value. ::before inserts it as the first child, before the element’s content, and ::after inserts it as the last child, after the content. Otherwise they behave identically.

Why is my ::before or ::after not showing?

The most common causes are a missing content property, applying it to a replaced element such as img or input that cannot have generated content, or an inline pseudo-element with no text that has no size. Add content: “” and set display or position with dimensions.

Do screen readers read CSS generated content?

Generally yes. Most browser and screen reader combinations announce text inserted with the content property. Mark decorative symbols with empty alternative text using the slash syntax, such as content: “→” / “”, and keep essential information in HTML.

Should I use a single colon or a double colon for pseudo-elements?

Use the double colon, which distinguishes pseudo-elements from pseudo-classes. Browsers still accept a single colon for the four oldest pseudo-elements, ::before, ::after, ::first-line and ::first-letter, but newer ones such as ::marker and ::backdrop require two.

Paint with CSS, speak with HTML

Pseudo-elements are the cleanest way to add the visual details that make interfaces feel finished, without filling the DOM with empty boxes. Keep them decorative, give generated symbols proper alternative text, and let HTML carry anything a user needs to read. What all these effects cost the browser, and how to ship them without layout shift, is covered in performance-safe CSS.

Rule of thumb: if removing the stylesheet would remove information, that information belongs in HTML.

Originally posted on 14 May 2026 and post last modified on 14 May 2026

Accessibility CSS CSS Selectors Front-End Development Pseudo-Elements

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *