:after - a pseudo-element that allows you to embed content on a page from CSS (without the need for HTML). Although the end result is not actually in the DOM, it appears on the page as if it were, and essentially would be:
div:after { content: "hi"; } <div> hi </div>
:before exactly the same as soon as it inserts the content before any other content in the HTML instead. The only reasons to use one over the other:
- You want the generated content to appear in front of the content of the element, positionally.
- The content
:after also βafterβ in the original order, so it will position on top of :: earlier if they are naturally stacked on top of each other.
The value for the content may be:
- String:
content: "a string"; - special characters must be specifically encoded as a unicode object. See the glyphs page. - Image:
content: url(/path/to/image.jpg); - The image is inserted into its exact dimensions and cannot be resized. Since things like gradients are images, a pseudo-element can be a gradient. - Nothing:
content: ""; - Useful for clearfix and pasting images as background images (set width and height and can even resize with background size). - Counter:
content: counter(li); - Really useful for style lists until a marker appears.
Please note that you cannot embed HTML (at least it will display as HTML). content: <h1>nope</h1> ;
source share