::before
Creates a virtual element at the start of an element’s content. Used for icons, decorations, and labels without extra HTML.
Example
#.alert {
display: flex;
align-items: center;
gap: 10px;
}
.alert::before {
content: "!";
flex-shrink: 0;
display: grid;
place-items: center;
width: 24px;
height: 24px;
border-radius: 50%;
background: #f59e0b;
color: #0c0c10;
font-weight: 800;
}<p class="alert stage" style="margin: 0;">Your fuel will refill tomorrow at midnight.</p>Definition and usage
#The ::before pseudo-element inserts a new first child into the element that does not exist in the HTML. It is not created before the element but inside it, ahead of all the other content. By default it is an inline element (inline), but you can give it any display, size, background, and position.
It exists only if the content property is set. Without it the pseudo-element is not generated at all, so even a purely decorative shape needs content: "". In content you can insert text, a symbol, an attribute value (attr(data-label)), or a counter.
It usually won’t work on elements that have no content of their own, such as <img>, <input>, or <br>, because there is no room in them for an extra child. The older single-colon syntax (:before) still works, but the double colon distinguishes pseudo-elements from pseudo-classes.
Text from content may be read by screen readers, but it can’t be selected or copied. Use it for decoration and keep important information in the HTML.
Syntax
#selector::before { content: …; }Common uses
#- A symbol before the text. The space inside the quotes separates it from the content.
- Content taken from the element’s
data-levelattribute. Change the attribute in the HTML and the label changes. - Empty content and explicit dimensions give you a status dot. Without
content: ""the dot would not be created. - A positioned pseudo-element draws a bar on the left edge, regardless of the text length.
Common uses in practice
#content: "★ "
.demo::before { content: "★ "; color: #fbbf24; }content: attr(data-level)
data-level attribute. Change the attribute in the HTML and the label changes..demo::before {
content: attr(data-level);
margin-right: 8px;
padding: 2px 6px;
border-radius: 6px;
background: #7c3aed;
font-size: 12px;
font-weight: 700;
}display: inline-block
content: "" the dot would not be created..demo::before {
content: "";
display: inline-block;
width: 10px;
height: 10px;
margin-right: 8px;
border-radius: 50%;
background: #10b981;
}position: absolute
.demo { position: relative; padding-left: 14px; }
.demo::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
border-radius: 4px;
background: #a78bfa;
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Every pseudo-element needs
content. If something doesn’t show up, first check that this declaration is in place. - When positioning
::beforeabsolutely, give the parentposition: relative. Otherwise the pseudo-element will be positioned relative to another ancestor. - For custom list bullets,
::markerorlist-style-typeis often more convenient. Keep::beforefor decorations that a list doesn’t provide.
Related entries
#- ::afterCreates a virtual element at the end of an element’s content. Ideal for arrows, badges, underlines, and overlays.
- contentInserts generated content in the ::before and ::after pseudo-elements.
- ::markerStyles a list item’s marker: the bullet in a ul list or the number in an ol list.
- ::first-letterStyles the first letter of the first line in a block of text. The classic way to make a drop cap at the start of an article.
- positionChooses how an element is positioned: in the flow, relative, absolute, fixed, or sticky.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.