::before
Example
.external::before {
width: 1em;
height: 1em;
background: url(icon.svg);
}.external::before {
content: '';
display: inline-block;
width: 1em;
height: 1em;
background: url(icon.svg);
}Without content the pseudo-element isn't created, and as an inline box it ignores width and height.
Common mistakes
- Nothing shows because the
contentproperty is missing. - Adding
::beforeto an<img>or<input>, where it doesn't render. - Setting
widthandheighton an inline::before, which ignores them.
Key points
- Every
::beforerule setscontent, at leastcontent: '', or nothing renders. - Essential text isn't placed in
content, since generated text can't be selected or copied. - Pseudo-elements are used on elements that can have children, not on
<img>or<input>.
Interview questions
Why is my ::before not showing?
Usually content is missing. Also check that the element can hold children, since <img>, <input> and other replaced elements don't render ::before.
Do screen readers read ::before content?
Often yes. Screen readers usually announce text from content, so decorative symbols can add noise.
What is the difference between :before and ::before?
None in behavior. The double colon is the current pseudo-element syntax; the single colon still works for compatibility.