What is the difference between ::before and ::after generated content?
Quick answer
Both ::before and ::after create generated content as immediate children of a selected element. ::before places it before that element’s content; ::after places it after. They’re useful for decoration, such as a small icon or quotation mark. Because generated content may not be available to assistive technology, put essential information in the HTML instead. [S1] [S2]
The question, the details, the next step.
A practical checklist
Choose
::beforefor decoration placed before the selected element’s content, or::afterfor decoration placed after it. [S1]Check whether the page’s meaning remains clear when CSS is disabled. [S2]
Use generated text for optional visual details, such as a decorative arrow or quotation mark. [S1]
Consider how the decoration fits the surrounding content; do not rely on it to carry a message people need.
A few more useful answers
Are `::before` and `::after` inserted into the HTML?
No. Their content is generated by CSS and is not part of the DOM. [S2] Keep information readers need in the document itself.
Can `::before` or `::after` be used on an image?
The cited reference says these pseudo-elements cannot be applied to replaced elements such as <img>. [S1] This guide’s example uses a link instead.
What does `content: none` do?
For these pseudo-elements, content: none prevents the pseudo-element from being generated; normal computes to none for ::before and ::after. [S2]
Sources
References used for this guide.
- [S1] MDN: ::after CSS pseudo-element developer.mozilla.org · Checked
- [S2] MDN: CSS content property and generated-content accessibility developer.mozilla.org · Checked
