Home CSS Selectors ::before CSS ::before Pseudo-element Definition
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.
Syntax
CSS Syntax selector::before { content : …; /* required to render */ }
Without a usable content value, the pseudo-element is not shown.
Patterns
Common Patterns Pattern Description Try it Text prefix content: "Note: " before the element text Try itEmpty box content: "" plus size/color for a decorative mark Try itQuotes Insert opening quote characters Try itUnicode Use an escape like \2605 for a star Try itattr()Pull a prefix from an HTML attribute Try it:beforeLegacy single-colon form (same idea) Try it
Approach
How to Use ::before Pick an element, set content, then style the generated box.
Choose a non-replaced element (p, h2, li, …). Write element::before { content: …; }. Add color, margin, or display as needed. Keep meaningful text in HTML for accessibility. Try it
Live Preview Change the content string applied to ::before. Empty string still renders if you keep a decorative box.
Code
CSS Examples Each snippet focuses on ::before plus content. Use Try It Yourself for a live editor.
1. Text prefix 2. Decorative box 3. Opening quote Example 1 — Text Prefix Insert a short label before a paragraph.
<style>
p::before {
content: "Note: ";
font-weight: 700;
color: #dc2626;
}
</style>
<p>Save your work often.</p> How It Works Target the element. p::before creates the first-child box.Set content. The string "Note: " is required to render.Style the prefix. Bold red only affects the generated text.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.
<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> How It Works content: "". Still creates a box, with no characters.Make it a blocklet. inline-block lets you set width and height.Paint the mark. Background color becomes a small square.Space it. margin-right separates the mark from the text.Example 3 — Opening Quote Add a decorative opening quotation mark before a quote.
<style>
q::before {
content: "«";
color: #2563eb;
margin-right: 0.15em;
}
</style>
<q>Practice makes progress.</q> How It Works Select q. The quote element gets a generated first child.Insert «. That character is only styling, not HTML text.Color the mark. Blue quote sits before the sentence.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 ☆.
stacking ::after paints laterWith the same stacking context, ::after content usually appears above ::before because it is rendered later.
Compatibility
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.
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.
Remember
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 What does CSS ::before do? It creates a pseudo-element as the first child of the selected element, usually to add decorative content with the content property.
Why must I set content? If content is missing, none, or normal, ::before is not rendered. Use a string, "", a url(), or attr().
What is the difference between ::before and ::after? ::before inserts generated content before the element’s content; ::after inserts it after.
Should I use ::before or :before? Prefer double-colon ::before for pseudo-elements. Single-colon :before still works for compatibility.
Can ::before style an img? No. Replaced elements like img and input generally cannot generate ::before / ::after. Wrap them instead.
Is ::before accessible for important text? Avoid putting critical information only in ::before. Screen readers do not reliably announce generated content.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion