Pseudo-elements

W3C CSS Snapshot 2026

A pseudo-element targets or creates a styleable part of an element. Its syntax begins with two colons.

On this page

Add decoration with ::before and ::after

::before and ::after create generated content around the element’s contents when content is set. An empty string creates an empty box you can style. Keep important information in HTML so it is available to readers and assistive tools.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Add decoration with ::before and ::after</title>
  <style>
    .notice::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; background: teal; }
    .notice::after { content: ""; display: inline-block; width: 16px; height: 2px; margin-left: 6px; background: teal; }
  </style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>

What to expect A small teal dot appears before “Your order is ready.” and a line appears after it. Remove content from either rule and its decoration disappears.

Style a part or match a state

::first-letter styles a part of a paragraph. A pseudo-class such as :hover matches a state of the whole element. You can combine a base selector with either one.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Style a part or match a state</title>
  <style>
    .notice::first-letter { font-weight: bold; font-size: 1.8em; }
    .notice:hover { color: purple; }
  </style>
</head>
<body>
<p class="notice">Your order is ready.</p>
<p>Your package is on the way.</p>
</body>
</html>

What to expect The first letter of Pen is larger. Hover the paragraph to change its color.

Official references