TExcelTAD
Home › CSS › CSS Pseudo-elements

CSS Pseudo-elements

A pseudo-element styles a specific part of an element — its first line, its first letter, the selected text — or inserts generated content before or after it. Pseudo-elements use a double colon: p::first-line, .card::before.

Note: Old CSS used a single colon (:before). Browsers still accept that for the four oldest pseudo-elements, but :: is the modern standard and distinguishes them from pseudo-classes.

Common pseudo-elements

Pseudo-element Targets
::before A generated child box inserted before the content
::after A generated child box inserted after the content
::first-letter The first letter of a block
::first-line The first rendered line of a block
::selection Text the user has highlighted
::marker The bullet or number of a list item
::placeholder Placeholder text of an input
::backdrop The layer behind a fullscreen element or modal <dialog>

::first-letter and ::first-line

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: Georgia, serif; max-width: 420px; line-height: 1.5; }
  p::first-letter { float: left; font-size: 3.4em; line-height: .9; margin: 4px 8px 0 0; color: #b71c1c; font-weight: bold; }
  p::first-line { font-variant: small-caps; letter-spacing: 1px; color: #37474f; }
  ::selection { background: #ffeb3b; color: #000; }
</style>
</head>
<body>
  <p>Once upon a time, a stylesheet learned to decorate the very first letter of a story. The first line got small capitals too. Try selecting some of this text with your mouse to see the custom selection colour.</p>
</body>
</html>

The first line adapts automatically — resize the playground and a different set of words becomes small caps.

::before and ::after

These insert a box as the first or last child of the element. They require the content property — even if it's just an empty string "" — or nothing is rendered. They're used for decorative icons, quotes, badges, overlays and shapes, keeping the HTML clean.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .new::after { content: "NEW"; margin-left: 8px; font-size: 11px; padding: 2px 6px; border-radius: 4px; background: #e91e63; color: white; vertical-align: middle; }
  .ext::after { content: " ↗"; color: #1e88e5; }
  blockquote { position: relative; padding: 10px 16px 10px 44px; background: #f3e5f5; border-radius: 8px; font-style: italic; }
  blockquote::before { content: "“"; position: absolute; left: 10px; top: -8px; font-size: 60px; color: #8e24aa; font-family: Georgia, serif; }
  .req::before { content: "* "; color: red; }
</style>
</head>
<body>
  <h3 class="new">Dark mode</h3>
  <p><a class="ext" href="#">External docs</a></p>
  <blockquote>Generated content keeps decoration out of the HTML.</blockquote>
  <p class="req">Email</p>
</body>
</html>

content values

Value Example Result
String content: "→ "; Literal text
Empty content: ""; An empty box you can size and colour
Attribute content: attr(data-tip); Text from an HTML attribute
Counter content: counter(step); A CSS counter value
Quotes content: open-quote; Language-appropriate quote marks
Image content: url(icon.svg); An image
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 30px 10px; }
  .tip { position: relative; border-bottom: 2px dotted #00897b; cursor: help; }
  .tip:hover::after {
    content: attr(data-tip);
    position: absolute; left: 0; bottom: 130%;
    background: #004d40; color: white; padding: 6px 10px; border-radius: 6px; white-space: nowrap; font-size: 14px;
  }
</style>
</head>
<body>
  <p>Hover the word <span class="tip" data-tip="Cascading Style Sheets">CSS</span> to read its attribute.</p>
</body>
</html>

Decorative shapes

With content: "" plus position: absolute, pseudo-elements become extra boxes for ribbons, underlines and overlays — two "free" elements per element.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 20px; }
  h2 { position: relative; display: inline-block; font-size: 30px; margin: 0 0 30px; }
  h2::after { content: ""; position: absolute; left: 0; bottom: -6px; width: 50%; height: 6px; border-radius: 3px; background: linear-gradient(90deg, #ff6f00, #ffca28); }
  .card { position: relative; width: 200px; height: 100px; background: #e1f5fe; border-radius: 10px; overflow: hidden; padding: 10px; }
  .card::before { content: "SALE"; position: absolute; top: 14px; right: -30px; transform: rotate(45deg); background: #d50000; color: white; padding: 4px 36px; font-weight: bold; font-size: 12px; }
</style>
</head>
<body>
  <h2>Underlined title</h2>
  <div class="card">A card with a ribbon</div>
</body>
</html>

Tip: Content in ::before/::after is not selectable and may be ignored or read differently by screen readers. Never put essential information there.

Note: ::before and ::after don't work on replaced elements such as <img> and <input>, because those have no content box to insert into.

Test yourself

What is required for ::before to render anything?

  1. display: block
  2. A content property
  3. position: absolute
  4. A data attribute

Which pseudo-element styles text the user highlights?

  1. ::highlight
  2. ::selected
  3. ::selection
  4. :focus

Why doesn't img::after show anything?

  1. Images are inline
  2. Replaced elements like img have no content box for generated content
  3. You need two colons
  4. It needs z-index