::before and ::after pseudo-elements in CSS
32 Generated content, before and after
fairAdd a decorative quotation mark before a pull quote and a “required” marker after a label, using pseudo-elements. Keep both out of the accessibility tree.
Stuck? Three hints, smallest first
- 1. No
content, no pseudo-element. Even an empty string counts:content: "". - 2. They attach inside the element, at the start and the end of its content.
- 3. They do not work on replaced elements:
<img>,<input>,<br>and friends have no content box to insert into.
<blockquote class="pull">
<p>The 06:30 leaves at 06:30. That is the whole reputation.</p>
</blockquote>
<p class="field">
<label class="req" for="phone">Phone</label>
<input id="phone" name="phone" type="tel" required>
</p>
.pull {
position: relative;
margin: 0 0 14px;
padding: 4px 0 4px 34px;
max-width: 300px;
font-size: 0.95rem;
}
/* Decorative, and announced to nobody: alt text on generated content is what
aria-hidden on the parent would be, so keep it purely visual. */
.pull::before {
content: '\201C'; /* a left double quotation mark */
position: absolute;
left: 0;
top: -4px;
font-size: 2.4rem;
line-height: 1;
color: #3055d8;
}
.pull p { margin: 0; }
.field { display: grid; gap: 4px; max-width: 240px; }
.req { font-weight: 600; font-size: 0.9rem; }
/* The marker is a hint, not the requirement. The input carries the required
attribute, so the real state is in the markup and this is only the cue. */
.req::after {
content: ' (required)';
color: #a3341f;
font-weight: 400;
}
.field input {
padding: 8px 10px;
border: 1px solid #8e8e98;
border-radius: 6px;
font: inherit;
}
.field input:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 1px;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('a ::before draws the quotation mark', /\.pull::before/.test(css), true);
test('and an ::after adds the required marker', /\.req::after/.test(css), true);
test('both declare content, without which they do not exist', (css.match(/content:/g) || []).length, 2);
test('the quote mark is a character escape, not typed inline', /content:\s*'\\201C'/.test(css), true);
test('the pseudo-element is attached to the label, not the input', /\.req::after/.test(css) && /input::after/.test(css) === false, true);
test('the real requirement lives on the field itself', /\brequired\b/.test(html), true);
test('the label is still joined to the input by for and id', /<label class="req" for="phone"/.test(html) && /id="phone"/.test(html), true);
test('a semantic blockquote holds the quote', hasTag(html, 'blockquote'), true);
Output
Two things worth carrying away. content is not optional — a ::before without it generates nothing at all, which is the most common reason a rule appears to be ignored. And a pseudo-element needs a content box to sit inside, so input::after generates nothing whatever you write: the marker has to go on the label, which is why the fifth check pins that down. Text you put in content is read out by some screen readers and not others, so it should never be the only place a fact lives: here the required attribute is the truth and the marker is only the cue.