CSS ::before Selector

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

What You’ll Learn

The ::before pseudo-element lets you insert content before an element’s real content using only CSS. It is ideal for icons, labels, quotation marks, and other decorative prefixes without changing your HTML.

01

Generated content

Add visuals in CSS.

02

content

Required property.

03

Icons & text

Stars, notes.

04

display

Block vs inline.

05

::after

Before vs after.

06

A11y

Decorative only.

Introduction

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

This is commonly used for decorative symbols before headings, “Note:” labels before paragraphs, link icons, opening quotation marks, and custom list markers — all without adding extra HTML tags.

Definition and Usage

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

By default, generated content is inline and flows on the same line as the element’s text. Use display: block when you need a prefix that sits on its own row.

💡
Beginner Tip

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

📝 Syntax

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

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

Basic Example

heading-star.css
h1::before {
  content: "★ ";
  color: #eab308;
  font-size: 1.1em;
}

p.note::before {
  content: "Note: ";
  font-weight: 700;
  color: #dc2626;
}

Syntax Rules

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

Related Selectors & Properties

  • ::after — inserts content after 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 usesIcons, labels, quotes, bullets
Browser supportAll modern browsers
h1::before p.note::before a::before blockquote::before

When to Use ::before

::before is best for presentational prefixes:

  • Heading icons — Add a star, arrow, or emoji before titles without an extra <span>.
  • Callout labels — Prefix paragraphs with “Note:” or “Tip:” for visual emphasis.
  • Link markers — Prepend 🔗 or other icons before anchor text.
  • Quotation marks — Add opening quotes before blockquotes.
  • Custom bullets — Replace default list markers with styled prefixes on li::before.

👀 Live Preview

A gold star before the heading, a red “Note:” before the paragraph, and a link icon before the anchor:

Welcome to My Website

This is an example of the ::before pseudo-element.

Visit Example Website

Examples Gallery

Try ::before with decorative symbols, generated text prefixes, link icons, and quotation marks.

📜 Core Patterns

Insert decorative symbols and text before existing HTML content.

Example 1 — Star before a heading

Add a gold star symbol before every <h1> using Unicode in content.

heading-star.css
h1::before {
  content: "★ ";
  color: #eab308;
  font-size: 1.1em;
}
Try It Yourself

How It Works

The star is inserted inline before the heading text. Unicode characters work directly inside content quoted strings.

Example 2 — “Note:” before a paragraph

Prefix important paragraphs with bold red text using a .note class.

note-prefix.css
p.note::before {
  content: "Note: ";
  font-weight: 700;
  color: #dc2626;
}
Try It Yourself

How It Works

Only paragraphs with the note class get the prefix. Screen readers may ignore decorative generated text, so keep the paragraph itself meaningful.

📄 UI Patterns

Use ::before for icons and typographic flourishes in real interfaces.

Example 4 — Opening quote before blockquote

Add a large opening quotation mark before blockquote text for a typographic effect.

blockquote-quote.css
blockquote::before {
  content: "\201C";
  color: #94a3b8;
  font-size: 2rem;
  line-height: 0;
  vertical-align: -0.25em;
  margin-right: 0.15rem;
}

blockquote {
  margin: 0;
  padding-left: 1rem;
  border-left: 3px solid #cbd5e1;
  color: #475569;
  font-style: italic;
}
Try It Yourself

How It Works

\201C is the Unicode escape for a left double quotation mark. Pair with ::after and \201D for a closing quote if desired.

⚠️ Common Pitfalls

  • Forgetting content — Without it, ::before does not render at all.
  • Void elements::before 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 ::before to distinguish pseudo-elements from pseudo-classes.

♿ Accessibility

  • Decorative only — Use ::before for visuals that do not convey unique meaning.
  • Keep real text in HTML — Important labels, errors, and instructions belong in the document markup.
  • Screen readers — Generated content is often ignored; do not rely on it for essential context.
  • Link icons — Ensure the link text itself still makes sense without the decorative icon.

🧠 How ::before Works

1

CSS defines ::before

You write a rule with content and optional styles.

Rule
2

Browser creates a pseudo-box

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

Generate
3

Content and styles render

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

Render
=

Richer design, same HTML

Visual polish without extra markup tags.

Browser Compatibility

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

Universal · All browsers

Generated content everywhere

::before 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
::before pseudo-element 99% supported

Bottom line: ::before is safe for decorative prefixes in any modern project.

Conclusion

The ::before pseudo-element is a versatile tool for inserting content before elements without modifying HTML. From heading stars to link icons and opening quotes, 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 ::before with ::after when you need decorations on both sides.

💡 Best Practices

✅ Do

  • Always include the content property
  • Use Unicode symbols for icons and bullets
  • Scope with classes for targeted prefixes
  • Keep generated content decorative or supplementary
  • Pair with ::after for balanced decorations

❌ Don’t

  • Put critical information only in ::before
  • 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 ::before with ::after placement

Key Takeaways

Knowledge Unlocked

Five things to remember about ::before

Use these points when adding generated prefixes.

5
Core concepts
📄 02

content

Required property.

Rule
03

Icons & labels

Stars, notes.

Uses
🔀 04

::after

Before vs after.

Pair
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The ::before pseudo-element inserts generated content immediately before an element's actual content. It is created purely in CSS without extra HTML.
Browsers only render ::before 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 ::before usually does not apply to them. Use a wrapper element instead.
Use the double-colon ::before syntax for pseudo-elements. Single-colon :before still works in older browsers but ::before is the modern standard.

Practice in the Live Editor

Open the HTML editor and experiment with ::before, 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