Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 139 Β· Selectors

Pseudo-elements

Learn CSS pseudo-elements, from ::before and ::after with content to ::first-letter, ::first-line, ::marker, ::placeholder, ::selection, ::backdrop and ::file-selector-button.

Pseudo-classes select whole elements in a particular state. Pseudo-elements do something different: they select a part of an element, or a box the browser creates for you, that doesn't exist as a tag in the HTML. The first line of a paragraph, the bullet of a list item, the placeholder text in an input, a decorative shape before a heading: all of these are pseudo-elements.

Pseudo-elements are written with two colons: p::first-line, li::marker, .card::before. This lesson starts with the two you'll use most, ::before and ::after, and then tours the rest.

Single colon or double colon?

Early CSS wrote pseudo-elements with one colon, like pseudo-classes: :before, :first-line. CSS3 introduced the double colon to tell the two apart. Browsers still accept the single-colon form for the four oldest ones (:before, :after, :first-line, :first-letter), but every newer pseudo-element requires ::. Use the double colon everywhere; it's clearer.

A pseudo-element must come last in a selector. .note::before:hover is invalid; write .note:hover::before for "the ::before of a hovered note".

::before and ::after

::before and ::after create a box as the first or last child of the element. They only appear when you give them a content property, even an empty one:

CSS
.tip::before {
  content: "πŸ’‘ ";
}

Without content, nothing is generated. That's the number one reason a ::before "doesn't work".

The content property accepts:

  • a string: content: "β†’ "
  • an empty string for purely decorative shapes: content: ""
  • an attribute value: content: attr(data-label)
  • a counter: content: counter(step) (see CSS Counters)
  • quotes: content: open-quote
  • an image: content: url(icon.svg)

The generated box is inline by default, like a <span>. Change its display to give it a width and height.

Text and shapes with ::before and ::after

The underline bar under the heading is a classic use: an empty content plus a width, height and background. No extra HTML required.

Generated content and accessibility

Text added with content is usually read by screen readers, but it can't be selected or copied, and it isn't part of the page's real text. Keep it decorative or supplementary. Important information belongs in the HTML.

For decorative symbols that screen readers shouldn't announce, modern browsers support alternative text after a slash. An empty alternative hides the symbol from assistive technology:

CSS
.external::after {
  content: "β†—" / "";            /* decorative: not announced */
}
.new::before {
  content: "β˜…" / "New: ";       /* announced as "New:" */
}

Browsers that don't understand the slash syntax ignore the whole declaration, so put a plain content: "β†—"; line before it as a fallback.

::before and ::after don't work on replaced elements such as <img>, <input> and <video>, because those elements have no content box for children to go into. Wrap them in a container and decorate that instead.

::first-letter and ::first-line

::first-letter targets the first letter of a block's text: perfect for drop caps. ::first-line targets whatever text happens to land on the first rendered line, which changes as the window resizes.

Drop cap and first-line styling

Resize the preview and watch the small caps follow the first line. Only a limited set of properties applies to these two (mostly font, color, background, spacing and, for ::first-letter, margins, padding and float). They work on block-level elements, not on inline ones like <span>.

::marker

::marker selects the bullet or number of a list item (anything with display: list-item). You can change its color, font and even its content:

CSS
li::marker {
  color: #db2777;
  font-weight: 700;
}

.checklist li::marker {
  content: "βœ“  ";
}

Only a few properties work on ::marker: color, font properties, content, white-space and a handful of others. For full control over layout, the Styling Lists lesson shows how to build custom markers with ::before.

::placeholder

::placeholder styles the hint text inside an empty <input> or <textarea>.

CSS
input::placeholder {
  color: #64748b;
  font-style: italic;
}

Keep placeholder text readable. Many default placeholder colors are too faint for comfortable reading, and placeholders should never replace a real <label>.

::selection

::selection styles text the user has highlighted with the mouse or keyboard. Only a few properties apply: mainly color, background-color and text decorations.

Markers, placeholders and selection

::backdrop

When an element is shown in the browser's top layer, such as a modal <dialog> opened with showModal(), a fullscreen element, or a popover, the browser draws a ::backdrop box behind it, covering the page. You can style it to dim or blur the content underneath:

Styling a dialog's ::backdrop

::backdrop only exists while the element is in the top layer. A dialog opened with plain show() isn't modal and gets no backdrop. Read more about dialogs in the dialog lesson.

::file-selector-button

<input type="file"> renders a button that was famously hard to style. ::file-selector-button selects that button directly:

CSS
input[type="file"]::file-selector-button {
  padding: 8px 14px;
  margin-right: 12px;
  border: 0;
  border-radius: 6px;
  background: #4f46e5;
  color: white;
  cursor: pointer;
}

input[type="file"]::file-selector-button:hover {
  background: #4338ca;
}

Notice the :hover after the pseudo-element. A small set of user-action pseudo-classes such as :hover are allowed after a pseudo-element; most others are not. This works in all current major browsers.

Debugging pseudo-elements

Generated boxes don't appear in your HTML source, but they do appear in the browser's DevTools. In the Elements panel, look inside the element for a ::before or ::after node; selecting it shows its rules in the Styles panel like any other element.

If a pseudo-element is missing, check these in order:

  1. Is content set? No content, no box.
  2. Is the element a replaced element such as <img> or <input>? Then ::before and ::after can't render.
  3. Is the box empty and still display: inline? An empty inline box has no width or height. Give it display: block or inline-block (or position it absolutely) plus a size.
  4. Is the selector valid? The pseudo-element must be the last part, and a typo like :before: drops the rule.

Specificity

Pseudo-elements count in the third column of specificity, the same as type selectors. p::first-line is (0, 0, 2), and .note::before is (0, 1, 1).

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. You wrote .badge::after { color: red; } but nothing appears. What is missing?

    ::before and ::after are only generated when content is set, even to an empty string.
  2. 2. Which element can NOT have a ::before pseudo-element?

    Replaced elements like <img> and <input> have no content box, so ::before and ::after don't render on them.
  3. 3. Which pseudo-element styles the bullets of a list?

    ::marker targets the marker box of any list item.
  4. 4. When does a <dialog> get a ::backdrop?

    ::backdrop is drawn behind elements in the top layer, which includes modal dialogs opened with showModal().
  5. 5. Which selector is valid?

    A pseudo-element must come at the end of the compound selector; the state goes on the element before it.

Summary

  • Pseudo-elements select parts of elements or generated boxes; write them with ::, at the end of a selector.
  • ::before and ::after need content; they are inline by default and don't work on replaced elements.
  • Keep generated content decorative, and use the / "" alternative-text syntax to hide symbols from screen readers.
  • ::first-letter and ::first-line style typographic parts of a block.
  • ::marker, ::placeholder and ::selection style bullets, hints and highlighted text, with a limited set of properties.
  • ::backdrop sits behind top-layer elements like modal dialogs; ::file-selector-button styles a file input's button.

Next: :is(), :where(), :not() and :has() β€” the selector functions that make complex selectors short and safe.