CSS ::after Selector

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Pseudo-elements

What You’ll Learn

The ::after pseudo-element lets you insert content after an element’s real content using only CSS. It is ideal for decorative lines, icons, and visual extras without changing your HTML.

01

Generated content

Add visuals in CSS.

02

content

Required property.

03

Decorations

Lines, icons, text.

04

display

Block vs inline.

05

::before

Before vs after.

06

A11y

Decorative only.

Introduction

The ::after selector in CSS is a pseudo-element used to insert content after an element’s actual content. It creates a virtual child that appears at the end of the selected element.

This is commonly used for decorative lines under headings, icons after links, quotation marks, badges, and other presentational touches — all without adding extra HTML tags.

Definition and Usage

Every ::after rule must include the content property. The value can be text in quotes, an empty string "" for shape-based decorations, or a function like attr(data-label).

By default, generated content is inline. Use display: block when you need a line or box that sits on its own row, such as an underline bar below a heading.

💡
Beginner Tip

If your ::after rule does nothing, check that content is set. Without it, the pseudo-element is not generated at all.

📝 Syntax

The signature of the ::after pseudo-element is:

syntax.css
element::after {
  content: " ";
  /* CSS properties */
}

Basic Example

after-line.css
h1::after {
  content: "";
  display: block;
  width: 50%;
  height: 2px;
  background-color: #2563eb;
  margin-top: 0.5rem;
}

Syntax Rules

  • Use double colon ::after for pseudo-elements (modern standard).
  • The content property is required.
  • Generated content is decorative by default — do not put essential information only in ::after.
  • Style it like any element: color, background, padding, border, etc.
  • Does not apply meaningfully to void elements like img and input.

Related Selectors & Properties

  • ::before — inserts content before the element
  • content — CSS property that defines generated text or empty placeholders
  • ::marker — styles list item bullets/numbers
  • content property — full reference for values like attr() and url()

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-element
Required propertycontent
Default displayInline
Empty decorative shapecontent: "";
Common usesLines, icons, badges, quotes
Browser supportAll modern browsers
h1::after p::after a.external::after label.required::after

When to Use ::after

::after is best for presentational enhancements:

  • Heading underlines — Add a colored bar below titles without an extra <span>.
  • Link indicators — Append ↗ or PDF icons to external links.
  • Form hints — Show a decorative asterisk on required labels (with proper HTML semantics too).
  • Quotes and flourishes — Add closing quotation marks to blockquotes.
  • Clearfix patterns — Historical layout technique using content: "" and display: table.

👀 Live Preview

A decorative line after the heading and italic text after the paragraph:

Welcome to My Website

This is an example paragraph.

External resource

Examples Gallery

Try ::after with decorative lines, generated text, link icons, and form markers.

📜 Core Patterns

Insert decorative shapes and text after existing HTML content.

Example 1 — Decorative line after a heading

Add a horizontal bar below an <h1> using empty content and display: block.

heading-line.css
h1::after {
  content: "";
  display: block;
  width: 50%;
  height: 2px;
  background-color: #2563eb;
  margin-top: 10px;
}
Try It Yourself

How It Works

content: "" creates an empty box you can size and color. display: block puts the line on its own row below the heading text.

Example 2 — Text after a paragraph

Append readable decorative text without editing the HTML paragraph.

paragraph-text.css
p::after {
  content: " - Thanks for reading!";
  color: #64748b;
  font-style: italic;
}
Try It Yourself

How It Works

Text in content is inserted inline at the end of the paragraph. Screen readers may ignore decorative generated text, so do not hide important meaning here alone.

📄 UI Patterns

Use ::after for icons and form markers in real interfaces.

Example 4 — Required field asterisk

Show a red asterisk after required labels for a clear visual cue.

required-label.css
label.required::after {
  content: " *";
  color: #dc2626;
  font-weight: 700;
}
Try It Yourself

How It Works

The asterisk is decorative. Also use the HTML required attribute and/or aria-required="true" so assistive technology understands the field is mandatory.

⚠️ Common Pitfalls

  • Forgetting content — Without it, ::after does not render at all.
  • Void elements::after does not work on img, input, br, and similar tags. Wrap them if needed.
  • Essential information — Do not put critical text only in generated content; screen readers may skip it.
  • Inline by default — For block-level decorations, set display: block or inline-block.
  • Single vs double colon — Prefer ::after to distinguish pseudo-elements from pseudo-classes.

♿ Accessibility

  • Decorative only — Use ::after for visuals that do not convey unique meaning.
  • Keep real text in HTML — Important labels, errors, and instructions belong in the document markup.
  • Required fields — Pair decorative asterisks with required and accessible label text.
  • External link icons — Ensure the link text itself still makes sense without the icon.

🧠 How ::after Works

1

CSS defines ::after

You write a rule with content and optional styles.

Rule
2

Browser creates a pseudo-box

A virtual box is inserted as the last child of the selected element.

Generate
3

Content and styles render

Text, icons, or empty shapes appear after the element’s real content.

Render
=

Richer design, same HTML

Visual polish without extra markup tags.

Browser Compatibility

The ::after pseudo-element is supported in all modern browsers. Very old browsers used the single-colon :after syntax.

Universal · All browsers

Generated content everywhere

::after works reliably in Chrome, Firefox, Safari, Edge, and Opera on desktop and mobile.

99% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support
::after pseudo-element 99% supported

Bottom line: ::after is safe for decorative enhancements in any modern project.

Conclusion

The ::after pseudo-element is a versatile tool for inserting content after elements without modifying HTML. From heading underlines to link icons, it keeps markup clean while giving you fine control over presentation.

Remember the golden rule: always set content, keep essential information in real HTML, and pair ::after with ::before when you need decorations on both sides.

💡 Best Practices

✅ Do

  • Always include the content property
  • Use content: "" for shape-based decorations
  • Set display: block for lines and bars
  • Keep generated content decorative or supplementary
  • Use ::after with classes for targeted effects

❌ Don’t

  • Put critical information only in ::after
  • Expect it to work on img or input directly
  • Forget that generated text may be ignored by screen readers
  • Overuse text in content for long paragraphs
  • Mix up ::after with ::before placement

Key Takeaways

Knowledge Unlocked

Five things to remember about ::after

Use these points when adding generated content.

5
Core concepts
📄 02

content

Required property.

Rule
🖼 03

Decorations

Lines, icons, text.

Uses
🔀 04

::before

Before vs after.

Pair
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The ::after pseudo-element inserts generated content immediately after an element's actual content. It is created purely in CSS without extra HTML.
Browsers only render ::after when content is defined. Use content with text, an empty string for decorative shapes, or functions like attr().
::before inserts content before the element's content. ::after inserts content after it. Both require the content property.
Void elements like img, input, and br do not have opening and closing content boxes in the same way, so ::after usually does not apply to them. Use a wrapper element instead.
Use the double-colon ::after syntax for pseudo-elements. Single-colon :after still works in older browsers but ::after is the modern standard.

Practice in the Live Editor

Open the HTML editor and experiment with ::after, content, and decorative pseudo-elements.

HTML Editor →

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.

5 people found this page helpful