Sub topic 11
Generated content with ::before and ::after
This chapter assumes you know CSS rule syntax, type, class and attribute selectors, and the difference between a pseudo-class and a pseudo-element (two colons for elements). It also assumes you know the box model and basic display values.
What ::before and ::after create
::before and ::after are pseudo-elements. Each one creates a box inside the selected element. ::before is inserted ahead of the element's content, and ::after follows it. The text of the page does not change. The browser adds the boxes only when it renders the page.
Both pseudo-elements do nothing unless the content property is set. Without content, no box is generated.
.external::after {
content: " \2197";
}The string " \2197" is a space followed by an arrow character. In CSS strings, a backslash and a hexadecimal code point write a character by its Unicode number.
The content property
content accepts several kinds of values:
| Value | Result |
|---|---|
| `"text"` | A literal string |
| `""` | An empty box, used for purely visual shapes |
| `attr(data-label)` | The value of an HTML attribute on the element |
| `counter(name)` | The current value of a CSS counter |
| `none` | No box is generated |
The attr() form is useful for showing information that is already in the markup:
a[href^="http"]::after {
content: " (" attr(href) ")";
}Decorative shapes with an empty string
An empty content creates a box you can size and color. The generated box is display: inline by default. Width and height have no effect on an inline box, so set display to block or inline-block first.
.card::before {
content: "";
display: block;
height: 4px;
background: #1a73e8;
}This draws a colored bar at the top of every .card. No extra HTML element is needed.
Where the box sits
The generated box is a child of the element, not a sibling. This has three consequences:
::beforeis placed before the element's first child, inside the element's padding.- Replaced elements such as
imgandinputhave no content area to hold children, so::beforeand::afterusually do not work on them. - The box inherits properties from the element, so it picks up the element's font and color unless you override them.
Common errors
- Forgetting
content. The rule applies but nothing appears. Addcontent: ""at minimum. - Using one colon.
:beforestill works in browsers for historical reasons, but write::beforeso pseudo-elements are easy to tell from pseudo-classes. - Setting size on an inline box. Width and height are ignored until
displaychanges. - Expecting to select the box from JavaScript. Pseudo-elements are not in the DOM. You cannot query them or attach event listeners to them. Style the parent instead.
- Putting essential text in
content. Users cannot select the generated text in most browsers, and assistive technology may read it inconsistently or skip it.
A complete example
This page adds a decorative bar to each card and an arrow to each external link.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Generated content</title>
<style>
.card {
border: 1px solid #ccc;
padding: 1rem;
max-width: 20rem;
}
.card::before {
content: "";
display: block;
height: 4px;
margin: -1rem -1rem 1rem;
background: #1a73e8;
}
.external::after {
content: " \2197";
}
</style>
</head>
<body>
<div class="card">
<h2>Docs</h2>
<p>Read the <a class="external" href="https://developer.mozilla.org/">MDN docs</a>.</p>
</div>
</body>
</html>The negative margins pull the bar out to the card's border edges. Remove the content line from the .card::before rule and the bar disappears.
Exercise
A rule .badge::after { width: 10px; height: 10px; background: red; } shows nothing. Name two changes needed to make a red square appear.
Show answer
Add content: "" so the box is generated, and add display: inline-block (or block) so the width and height apply.
Exercise
Why should a product's price not be written only as .price::after { content: "$19.99"; }?
Show answer
Generated text is not part of the DOM. Users cannot reliably select it, and screen readers may skip it or read it inconsistently, so the price should be in the HTML.