Favorites

CSS Pseudo Elements

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
CSS Pseudo Elements

What is a pseudo-element?

In CSS, a selector is the part of a rule that picks which elements to style. For example, p selects every paragraph. Normally you can only style whole elements, which are the tags you've written in your HTML.

A pseudo-element lets you style a specific part of an element, or even add new content that isn't in your HTML at all. "Pseudo" means "pretend": the browser treats these parts as if they were real elements, even though you never wrote them.

With pseudo-elements you can:

  • style the first letter or first line of a paragraph
  • add icons, quote marks or decorations without touching your HTML
  • colour list bullets
  • change how highlighted text looks
  • style the placeholder text inside form fields

Writing a pseudo-element

You write a pseudo-element by adding two colons and its name after a selector:

p::first-letter {
  color: green;
}

Read that as "the first letter of every paragraph". The part before the colons picks the element, and the part after picks which piece of it to style.

One colon or two? Older tutorials (including the first version of this post) wrote pseudo-elements with a single colon, such as :before. Modern CSS uses two colons (::before) to tell them apart from pseudo-classes like :hover, which style an element in a particular state. Browsers still accept the single-colon form for the four original pseudo-elements, but use two colons in new code.

::first-letter

The ::first-letter pseudo-element styles the first letter of a block of text, such as a paragraph or heading. This example makes the first letter green and twice the normal size:

p::first-letter {
  font-size: 2em;
  color: green;
}
Results

Once upon a time, a web designer discovered pseudo-elements and never looked back.

Making a drop cap

A drop cap is a large first letter that sinks down into the paragraph, a style borrowed from old books and magazines. Floating the letter to the left lets the text wrap around it:

p::first-letter {
  float: left;
  font-size: 3.4em;
  line-height: 0.9;
  font-weight: 700;
  color: #1d4ed8;
  padding: 4px 8px 0 0;
}
Results

Long before screens, printers decorated the opening of a chapter with an oversized letter. CSS lets you recreate that look with a single rule, and the text flows neatly around it.

Good to know: ::first-letter only works on block-level elements such as paragraphs, headings and list items. It won't work on an inline element like a <span> unless you change it to display: block or inline-block.

::first-line

The ::first-line pseudo-element styles the first line of a block of text. This example makes the first line italic and green:

p::first-line {
  font-style: italic;
  color: green;
}
Results

This first line is styled differently from the rest. Resize your browser window and watch: the styling follows whatever words fit on the first line, because the browser works it out each time the layout changes.

That's what makes ::first-line clever. You don't need to know where the line ends; the browser does that for you, even on a small phone screen.

Tip: only some properties work with ::first-line, mainly those that affect text: colour, font, letter spacing, text decoration and background. Properties like margin or padding are ignored.

::before and ::after

These two are the most useful pseudo-elements of all. ::before inserts content at the start of an element, inside it, and ::after inserts content at the end. The inserted content is called generated content because CSS creates it, not your HTML.

What gets inserted is set with the content property. This example adds a heart to the start of each paragraph:

p::before {
  content: "\2665  ";
  color: #dc2626;
}
Results

I love CSS.

Pseudo-elements are great.

And this one adds a heart to the end instead:

p::after {
  content: "  \2665";
  color: #dc2626;
}
Results

I love CSS.

Pseudo-elements are great.

What's that \2665?

2665 is the Unicode code for the ♥ symbol. Unicode is the worldwide standard that gives every character, letter and emoji its own number. In CSS you write a Unicode character with a backslash in front, so "\2665" becomes ♥. Without the backslash, the browser would print the digits "2665" instead.

Since your page uses UTF-8 (that's what the <meta charset="utf-8"> tag does), you can also paste the symbol directly: content: "♥";. The escaped version is handy for characters that are hard to type.

What can content hold?

ValueWhat it inserts
content: "Note: ";A piece of text
content: "\2192";A Unicode character (here, an arrow →)
content: url("icon.svg");An image
content: attr(data-label);The value of an HTML attribute on the element
content: open-quote;A quotation mark suited to the page language
content: "";Nothing visible, but creates a box you can style as a shape

Key rule: ::before and ::after don't appear at all without a content property. Even if you only want a decorative shape, you must write content: "";.

Practical ::before and ::after recipes

Adding hearts is fun, but in real projects these pseudo-elements do a lot of everyday work. Here are three patterns you'll use again and again.

Marking required form fields

Instead of typing an asterisk into every label, let CSS add it. Here the HTML gives the label a required class:

<label class="required" for="email">Email address</label>
label.required::after {
  content: " *";
  color: #dc2626;
  font-weight: 700;
}
Results

Decorative quote marks

A large, coloured quotation mark makes a <blockquote> stand out. Giving the blockquote position: relative lets you place the mark exactly with position: absolute:

blockquote {
  position: relative;
  padding-left: 44px;
  font-style: italic;
}
blockquote::before {
  content: "\201C"; /* the curly opening quote mark */
  position: absolute;
  left: 0;
  top: -10px;
  font-size: 3.5em;
  line-height: 1;
  color: #1d4ed8;
}
Results
Good design is as little design as possible.

An animated underline

With content: "", a pseudo-element becomes an empty box you can shape however you like. Here it's a thin bar under a link that grows from the left when you hover:

a {
  position: relative;
  text-decoration: none;
}
a::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -3px;
  width: 100%;
  height: 3px;
  background: #1d4ed8;
  transform: scaleX(0);         /* squashed to nothing */
  transform-origin: left;
  transition: transform 0.3s ease;
}
a:hover::after {
  transform: scaleX(1);         /* full width on hover */
}

Notice a:hover::after: you can combine a pseudo-class with a pseudo-element. It reads as "the ::after part of a link, while it's hovered".

::marker for list bullets

The ::marker pseudo-element styles the bullet or number at the start of each list item. Before it existed, changing a bullet's colour meant removing the bullets and rebuilding them with ::before. Now it's one rule:

li::marker {
  color: #1d4ed8;
  font-weight: 700;
}
Results
  1. Blue, bold numbers
  2. The list text stays unchanged
  3. No extra HTML needed

Good to know: ::marker supports only a small set of properties, mainly colour, font settings and content. For fully custom bullets with backgrounds or exact positioning, ::before is still the tool to use.

::selection, ::placeholder and form pseudo-elements

::selection

::selection styles text while a visitor highlights it with their mouse or finger. It's a small touch that can match the highlight to your brand colours. Try selecting the text in the Results box:

::selection {
  background: #fde047;
  color: #111827;
}
Results

Select this sentence to see a yellow highlighter effect instead of the browser's default colour.

Writing ::selection on its own applies it to the whole page. Put a selector in front, as in p::selection, to limit it.

::placeholder

A placeholder is the hint text shown inside an empty form field, set with the placeholder attribute in HTML. ::placeholder lets you style it:

input::placeholder {
  color: #1d4ed8;
  font-style: italic;
  opacity: 0.7;
}
Results

Tip: keep placeholder text readable, with enough contrast against the background. A placeholder is also no substitute for a proper <label>, because it disappears the moment someone starts typing.

::file-selector-button

File upload fields (<input type="file">) used to be almost impossible to style. ::file-selector-button targets the "Choose file" button inside them:

input[type="file"]::file-selector-button {
  background: #1d4ed8;
  color: #ffffff;
  border: 0;
  border-radius: 6px;
  padding: 8px 14px;
  margin-right: 12px;
  cursor: pointer;
}
Results

::backdrop

When you open a modal <dialog> or show something in full-screen mode, the browser places a layer behind it covering the rest of the page. ::backdrop styles that layer, for example dimming and blurring the page behind a popup:

dialog::backdrop {
  background: rgb(0 0 0 / 0.5);
  backdrop-filter: blur(4px);
}

Quick reference and common mistakes

Pseudo-elementWhat it targets
::first-letterThe first letter of a block of text
::first-lineThe first line of a block of text
::beforeGenerated content at the start of an element
::afterGenerated content at the end of an element
::markerThe bullet or number of a list item
::selectionText the visitor has highlighted
::placeholderHint text in an empty form field
::file-selector-buttonThe button in a file upload field
::backdropThe layer behind a modal dialog or full-screen element

Mistakes to watch out for

  • Forgetting content. ::before and ::after stay invisible without it. Use content: "" for purely decorative shapes.
  • Using them on images and inputs. Elements like <img>, <input> and <br> have no inner content, so ::before and ::after don't work on them. Wrap them in another element and style that instead.
  • Missing the backslash. content: "2665" prints the number; content: "\2665" prints the heart.
  • One pseudo-element per selector. You can't chain them, so p::before::first-letter won't work. Pseudo-classes, however, can come before them, as in a:hover::after.

Keep important content in your HTML

Generated content isn't part of your page's real text. It can't be selected or copied in most browsers, search engines may ignore it, and screen readers (software that reads pages aloud for people with low vision) don't always announce it consistently.

Key rule: use pseudo-elements for decoration and visual extras, such as icons, quote marks and flourishes. If the information matters to understanding the page, put it in your HTML.

Wrapping up

Pseudo-elements let you style parts of an element and add extra content, all without cluttering your HTML. Start with ::before and ::after, since you'll use them constantly. Then reach for ::marker, ::placeholder and ::selection whenever you want those finishing touches.

In a future post we'll look at pseudo-classes such as :hover, :focus and :nth-child(), which style elements based on their state or position.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee