{"id":14158,"date":"2011-09-06T19:51:23","date_gmt":"2011-09-07T02:51:23","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=14158"},"modified":"2022-09-30T12:06:53","modified_gmt":"2022-09-30T19:06:53","slug":"after-and-before","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/a\/after-and-before\/","title":{"rendered":"::before \/ ::after"},"content":{"rendered":"\n
The The only reasons to use one over the other are:<\/p>\n\n\n\n Note that the content is still inside<\/em> the element they are applied to. The naming sort of feels like they might come, ya know, before or after the element, but it’s really before or after the other content inside<\/em>.<\/p>\n\n\n\n The value for content can be:<\/p>\n\n\n\n::before<\/code> and
::after<\/code> pseudo-elements in CSS allows you to insert content onto a page without it needing to be in the HTML. While the end result is not actually in the DOM, it appears on the page as if it is, and would essentially be like this:<\/p>\n\n\n\n
div::before {\n content: \"before\";\n}\ndiv::after {\n content: \"after\";\n}<\/code><\/pre>\n\n\n\n
<div>\n before\n <!-- Rest of stuff inside the div -->\n after\n<\/div><\/code><\/pre>\n\n\n\n
::after<\/code> content is also “after” in source-order, so it will position on top of ::before if stacked on top of each other naturally.<\/li><\/ul>\n\n\n\n
content: \"a string\";<\/code> \u2014 special characters need to be specially encoded as a unicode entity. See the glyphs page<\/a>.<\/li>
content: url(\/path\/to\/image.jpg);<\/code> \u2014 The image is inserted at it’s exact dimensions and cannot be resized.<\/a> Since things like gradients<\/a> are actually images, a pseudo element can be a gradient.<\/li>
content: \"\";<\/code> \u2014 Useful for clearfixes<\/a> and inserting images as a
background-image<\/code> (set
width<\/code> and
height<\/code>, and can even resize with
background-size<\/code>).<\/li>
content: counter(li);<\/code> \u2014 Really useful for styling lists<\/a> (but we also have
::marker<\/a><\/code> for that).<\/li>
content: \"Killing \\A Me \\A Softly\";<\/code> \u2014 Great for when you really need one<\/a>.<\/li><\/ul>\n\n\n\n