CSS ::before Pseudo-element

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

Definition and Usage

The ::before pseudo-element creates a box that acts like the first child of the selected element. It is usually used with the content property to add decorative text or shapes.

It is inline by default. Prefer double-colon ::before (Selectors Level 3). Single-colon :before still works for older CSS2 syntax.

Baseline · Widely available.

Not deprecated. Cross-browser since about July 2015.

Accessibility.

Do not put important information only in ::before. Screen readers may skip generated content. Keep meaning in real HTML.

CSS Syntax

syntax.css
selector::before { content: …; /* required to render */ }

Without a usable content value, the pseudo-element is not shown.

Common Patterns

PatternDescriptionTry it
Text prefixcontent: "Note: " before the element textTry it
Empty boxcontent: "" plus size/color for a decorative markTry it
QuotesInsert opening quote charactersTry it
UnicodeUse an escape like \2605 for a starTry it
attr()Pull a prefix from an HTML attributeTry it
:beforeLegacy single-colon form (same idea)Try it

How to Use ::before

Pick an element, set content, then style the generated box.

  1. Choose a non-replaced element (p, h2, li, …).
  2. Write element::before { content: …; }.
  3. Add color, margin, or display as needed.
  4. Keep meaningful text in HTML for accessibility.

Live Preview

Change the content string applied to ::before. Empty string still renders if you keep a decorative box.

Generated text appears before “Hello”. none removes the pseudo-element.

Sample

Hello

content: "★ "

CSS Examples

Each snippet focuses on ::before plus content. Use Try It Yourself for a live editor.

Example 1 — Text Prefix

Insert a short label before a paragraph.

CSS
<style>
  p::before {
    content: "Note: ";
    font-weight: 700;
    color: #dc2626;
  }
</style>

<p>Save your work often.</p>
Try It Yourself

How It Works

  1. Target the element. p::before creates the first-child box.
  2. Set content. The string "Note: " is required to render.
  3. Style the prefix. Bold red only affects the generated text.
  4. Real text stays in HTML. “Save your work often.” remains accessible.

Example 2 — Decorative Box

Use an empty string, then size and color a marker before the text.

CSS
<style>
  .item::before {
    content: "";
    display: inline-block;
    width: 0.65rem;
    height: 0.65rem;
    margin-right: 0.45rem;
    background: #2563eb;
  }
</style>

<p class="item">Feature highlight</p>
Try It Yourself

How It Works

  1. content: "". Still creates a box, with no characters.
  2. Make it a blocklet. inline-block lets you set width and height.
  3. Paint the mark. Background color becomes a small square.
  4. Space it. margin-right separates the mark from the text.

Example 3 — Opening Quote

Add a decorative opening quotation mark before a quote.

CSS
<style>
  q::before {
    content: "«";
    color: #2563eb;
    margin-right: 0.15em;
  }
</style>

<q>Practice makes progress.</q>
Try It Yourself

How It Works

  1. Select q. The quote element gets a generated first child.
  2. Insert «. That character is only styling, not HTML text.
  3. Color the mark. Blue quote sits before the sentence.
  4. Often pair with ::after. Closing marks usually use ::after.

Edge Cases & Pitfalls

Most “broken” ::before rules are missing content or targeting the wrong element type.

content

No content, no box

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

replaced

Replaced elements skip it

img, input, and similar replaced elements generally cannot generate ::before. Wrap them in a span or div.

a11y

Not for critical text

Generated content is unreliable for screen readers. Put labels and warnings in real markup.

entities

No HTML entities in content

Use a literal character or a Unicode escape (\2605), not &star;.

stacking

::after paints later

With the same stacking context, ::after content usually appears above ::before because it is rendered later.

Browser Support

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

✓ Baseline · Widely available

::before

Supported in all modern browsers and IE 8+ (as :before / ::before). Widely available since July 2015.

99% Browser support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 4+
Full support
Samsung Internet 1+
Full support
Internet Explorer 8+
Full support
::before Widely supported

Bottom line: Safe for decorative UI everywhere. Prefer ::before spelling. It is not a runtime API.

Key Takeaways

  • Role: first-child generated box before the element’s content.
  • Needs content: set content or nothing appears.
  • Decorative: icons and marks yes; critical copy no.
  • vs ::after: before vs after the real content.

One line: use ::before with content to decorate the start of an element.

Frequently Asked Questions

It creates a pseudo-element as the first child of the selected element, usually to add decorative content with the content property.
If content is missing, none, or normal, ::before is not rendered. Use a string, "", a url(), or attr().
::before inserts generated content before the element’s content; ::after inserts it after.
Prefer double-colon ::before for pseudo-elements. Single-colon :before still works for compatibility.
No. Replaced elements like img and input generally cannot generate ::before / ::after. Wrap them instead.
Avoid putting critical information only in ::before. Screen readers do not reliably announce generated content.

Next: ::first-letter

Style the first letter of a block for drop-cap effects.

::first-letter 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