In CSS, ::before
creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content
property. It is inline by default.
In CSS, ::before
creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content
property. It is inline by default.
Note: The pseudo-elements generated by ::before
and ::after
are contained by the element's formatting box, and thus don't apply to replaced elements such as <img>
, or to <br>
elements.
::before { /* ... */ }
Note: Selectors Level 3 introduced the double-colon notation ::before
to distinguish pseudo-classes from pseudo-elements. Browsers also accept single-colon notation:before
, introduced in CSS2.
One simple example of using ::before
pseudo-elements is to provide quotation marks. Here we use both ::before
and
to insert quotation characters.::after
<q>Some quotes</q>, he said, <q>are better than none.</q>
q::before { content: "«"; color: blue; } q::after { content: "»"; color: red; }
We can style text or images in the content
property almost any way we want.
<span class="ribbon">Notice where the orange box is.</span>
.ribbon { background-color: #5bc8f7; } .ribbon::before { content: "Look at this orange box."; background-color: #ffba10; border-color: black; border-style: dotted; }
In this example we will create a simple to-do list using pseudo-elements. This method can often be used to add small touches to the UI and improve user experience.
<ul> <li>Buy milk</li> <li>Take the dog for a walk</li> <li>Exercise</li> <li>Write code</li> <li>Play music</li> <li>Relax</li> </ul>
li { list-style-type: none; position: relative; margin: 2px; padding: 0.5em 0.5em 0.5em 2em; background: lightgrey; font-family: sans-serif; } li.done { background: #ccff99; } li.done::before { content: ""; position: absolute; border-color: #009933; border-style: solid; border-width: 0 0.3em 0.25em 0; height: 1em; top: 1.3em; left: 0.6em; margin-top: -1em; transform: rotate(45deg); width: 0.5em; }
const list = document.querySelector("ul"); list.addEventListener( "click", (ev) => { if (ev.target.tagName === "LI") { ev.target.classList.toggle("done"); } }, false );
Here is the above code example running live. Note that there are no icons used, and the check-mark is actually the ::before
that has been styled in CSS. Go ahead and get some stuff done.
As this is CSS; not HTML, you can not use markup entities in content values. If you need to use a special character, and can not enter it literally into your CSS content string, use a unicode escape sequence, consisting of a backslash followed by the hexadecimal unicode value.
<ol> <li>Crack Eggs into bowl</li> <li>Add Milk</li> <li>Add Flour</li> <li aria-current="step">Mix thoroughly into a smooth batter</li> <li>Pour a ladleful of batter onto a hot, greased, flat frying pan</li> <li>Fry until the top of the pancake loses its gloss</li> <li>Flip it over and fry for a couple more minutes</li> <li>serve with your favorite topping</li> </ol>
li { padding: 0.5em; } li[aria-current="step"] { font-weight: bold; } li[aria-current="step"]::after { content: " \21E6"; /* Hexadecimal for Unicode Leftwards white arrow*/ display: inline; }
Using a ::before
pseudo-element to add content is discouraged, as it is not reliably accessible to screen readers.
Specification |
---|
CSS Pseudo-Elements Module Level 4 # generated-content |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
::before |
11 | 1212 |
1.5["Before Firefox 57, Firefox had a bug where::before pseudo-elements were still generated, even if the content property value were set to normal or none .", "Before Firefox 3.5, only the CSS level 2 behavior of :before was supported, which disallowed position , float , list-style-* and some display properties."] |
98 | 74 | 44 | ≤37≤37 | 1818 |
4Before Firefox 57, Firefox had a bug where::before pseudo-elements were still generated, even if the content property value were set to normal or none . |
10.110.1 | 33 | 1.01.0 |
animation_and_transition_support |
26 | 12 | 4 | No | 15 | No | 4.4 | 26 | 4 | 14 | No | 1.5 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/::before