CSS ::after Pseudo-Element

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

::after creates a pseudo-element that acts as the last child of the selected element. It is inline by default and is usually paired with the content property for icons, labels, or decoration.

Accessibility Prefer real HTML for meaning

Generated ::after text is not reliably read by screen readers. Keep important information in the document, not only in CSS.

Note content is required to show

If content is omitted, invalid, normal, or none, the pseudo-element is not rendered.

CSS Syntax

syntax.css
selector::after { content: …; … }

Prefer double-colon ::after. The older single-colon :after still works in browsers.

Selector Patterns

PatternDescriptionTry it
p::after { content: " …"; }Append text after a paragraphTry it
.box::after { content: ""; }Empty string for a decorative shapeTry it
a::after { content: " (" attr(href) ")"; }Show a link’s href after the link textTry it
::before vs ::afterContent before vs after the element’s real textTry it

How to Use ::after

Pick an element, attach ::after, then set content.

  1. Choose a non-replaced element (not img / input).
  2. Write selector::after { … }.
  3. Set content to a string, "", or attr(…).
  4. Add layout styles only as needed (display, color, size).
PieceRequired?Role
Originating elementYesReal element that owns the pseudo-element
::afterYesCreates the last-child pseudo box
contentYes (to show)Defines what is generated
Other propertiesOptionalColor, size, position, display

Live Preview

The green label after the sentence comes from ::after—not from extra HTML text.

::after demo

Inspect the markup: only one <p>. The suffix is CSS.

::after preview

Contributing to MDN is fun

Rule used: .tut-aft-demo::after { content: " <- EXCITING!"; }

CSS Examples

Each snippet focuses on ::after and content. Use Try It Yourself for a live editor.

Example 1 — Append Text

Add a short label after a paragraph (MDN-style demo).

CSS
.exciting::after {
  content: " <- EXCITING!";
  color: darkgreen;
  font-weight: bolder;
}
Try It Yourself

How It Works

  1. Select the element. .exciting is the originating paragraph.
  2. Attach ::after. A last-child pseudo box is created.
  3. Set content. The string becomes the generated text.
  4. Style it. Color and weight apply only to that pseudo text.

Example 2 — Decorative Box

Use an empty string so you can draw a shape with box styles.

CSS
.ribbon::after {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-left: 6px;
  background: #ffba10;
  vertical-align: middle;
}
Try It Yourself

How It Works

  1. Use content: "". The pseudo-element exists but has no text.
  2. Switch display. inline-block lets you set width and height.
  3. Paint the box. Background color makes the square visible.
  4. Keep meaning in HTML. The square is decoration only.

Example 3 — Show a Link URL

Pull an attribute into generated content with attr().

CSS
a::after {
  content: " (" attr(href) ")";
  font-size: 0.85em;
  color: #64748b;
}
Try It Yourself

How It Works

  1. Target links. a::after runs on each anchor.
  2. Read the attribute. attr(href) copies the href value.
  3. Wrap it in a string. Parentheses make the URL easy to scan.
  4. Print use case. Handy for print stylesheets; keep a11y needs in mind on screen.

Edge Cases & Pitfalls

Most “::after is broken” bugs are missing content or a replaced element.

content

No content, no box

Omitting content, or using none / normal, hides the pseudo-element entirely.

replaced

Replaced elements ignore it

img, input, and similar replaced elements cannot host ::after. Style a wrapper instead.

a11y

Do not hide critical text in CSS

Required labels, errors, and instructions belong in HTML so assistive tech can read them.

stack

::after paints after ::before

With no z-index, ::after content sits above ::before because it comes later in the flow.

syntax

Prefer ::after over :after

Double colon marks a pseudo-element. Single colon still works for compatibility.

Browser Support

::after is a browser CSS pseudo-element. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

::after

Supported in all major browsers for decades (Baseline Widely available). Safe for decorative UI patterns.

99% All modern browsers
Google Chrome All
Full
Mozilla Firefox All
Full
Apple Safari All
Full
Microsoft Edge All
Full
Opera All
Full
Samsung Internet All
Full
Internet Explorer 8+
Full
selector::after Universally supported

Bottom line: Production-ready. Prefer ::after (double colon), always set content, and keep meaningful text in HTML.

Key Takeaways

  • Role: last-child pseudo-element for generated content after real text.
  • Must-have: set content or nothing appears.
  • vs ::before: after the content vs before it.
  • Tip: decoration only—keep meaning in HTML for accessibility.

One line: el::after { content: "…"; } draws content after the element’s real children.

Frequently Asked Questions

It creates a pseudo-element as the last child of the selected element. You style it with content and other properties to show text or decoration after the real content.
If content is missing, invalid, normal, or none, the ::after box is not rendered. Use a string, "", attr(), or similar so it can appear.
Prefer the double-colon ::after form for pseudo-elements. Browsers still accept the older single-colon :after from CSS2.
No. Replaced elements like img and input do not support ::before / ::after. Wrap them and style the wrapper instead.
Not reliably. Screen readers may skip generated content. Keep important meaning in real HTML text, not only in ::after.
No. It is a browser CSS pseudo-element, not a Node.js feature.

Next: ::before

Learn the matching pseudo-element that inserts content before an element.

::before tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful