{"id":9516,"date":"2011-06-13T11:42:39","date_gmt":"2011-06-13T18:42:39","guid":{"rendered":"http:\/\/css-tricks.com\/?p=9516"},"modified":"2021-08-19T11:50:26","modified_gmt":"2021-08-19T18:50:26","slug":"pseudo-element-roundup","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/pseudo-element-roundup\/","title":{"rendered":"A Whole Bunch of Amazing Stuff Pseudo Elements Can Do"},"content":{"rendered":"\n
It’s pretty amazing what you can do with the pseudo-elements Because you can absolutely position pseudo-elements relative to their parent element, you can think of them as two extra layers to play with for every element. Nicolas Gallagher shows us<\/a> lots of applications of this, including multiple borders, simulating CSS3 multiple backgrounds, and equal height columns.<\/p>\n\n\n All of the shapes above and many more<\/a> can be created with a single element, which makes them actually practical. As opposed to those “make a coffee cup with pure CSS!” things that use 1,700 divs, which are neat but not practical.<\/p>\n\n\n\n Here’s an example of a six-pointed star:<\/p>\n\n\n\n Rather than insert extra non-semantic markup to clear the float on container elements, we can use a pseudo-element to do that for us. Commonly known as the “clearfix”<\/a>, and more semantically titled with the class name “group”.<\/p>\n\n\n\n The float<\/a> property doesn’t actually have a value of center, despite how much we might want it. Float does have left and right though, and by using placeholder pseudo elements we can carve out an area between two columns and place an image there, we can simulate the effect<\/a>.<\/p>\n\n\n In the current design of this very site, the blocks of code are labeled with what language of code they are with a pseudo-element:<\/p>\n\n\n\n Nicolas Gallagher again taking the idea of the shapes to another level by creating a massive set of icons created with no images, only pseudo-elements on (at most) two elements each.<\/p>\n\n\n What CSS giveth, CSS can taketh away. What I mean is that pseudo-element content can be applied or removed conditionally via media queries<\/a>. Perhaps you apply an icon when space is limited, and replace that with a more descriptive word when there is more room.<\/p>\n\n\n If your pseudo-elements are text, they inherit the same typographic styles as their parent element. But while you are setting their content, you can style them as well. Like, say, use a different font and a different color to make your headers stand out with a flourish.<\/p>\n\n\n\n When you need elements whose background stretches full width but whose content does not, you are often stuck with non-semantic internal wrapping elements or repetitive and cluttery spacing declarations. Or you can simulate the effect<\/a> by limiting the content width with one element and making the header bar reach out to the edges with pseudo-elements.<\/p>\n\n\n Using a regular border on the left and right, and fixed position pseudo-element bars on the top and bottom, we can get a “body border” effect<\/a> without any dedicated markup at all.<\/p>\n\n\n\n If you make a pseudo-element block that has a transparent to white to transparent gradient on it, position it outside of the button (which hides it with hidden overflow), and transition-move it across the button on hover, you can make a button that seems to catch a bit of light as you mouse over it. Only supported for Firefox, Chrome 26+, and IE10+.<\/p>\n\n\n\n Original<\/a> by Anton Trollb\u00e4ck; Pseudoelementized<\/a> by Nicolas Gallagher; Another version<\/a> by me.<\/p>\n\n\n If you don’t set relative positioning on an element, absolutely positioned pseudo-elements will be positioned relative to the next parent that does. If nothing else does, it will be relative to the root element. You can use that to make a full-browser-window element, stack it underneath the main element, and reveal it on hover making a “page fade out” effect<\/a> on a link.<\/p>\n\n\n Everybody loves ribbons! Check out this snippet<\/a> for the HTML and CSS for this one. It makes use of negative Have you ever tried to style the numbers in an ordered list? You end up doing dumb stuff like wrapping the insides in spans, styling the list items, then removing that styling with the span. Or using background images in crazy ways. Or removing the list styling and putting in your own numbers. All those ways suck. Much better to utilize pseudo-elements as counters<\/a>.<\/p>\n\n\n Large data tables are awful to browse on small screens. Either they are zoomed in and require both vertical and horizontal scrolling, or they are zoomed out and too small to read. We can combine CSS media queries with pseudo-elements to make the data table responsive<\/a> and reformat it to a more readable view when on small screens.<\/p>\n\n\n Using an HTML5 data attribute, then pulling that attribute in and styling it as a pseudo-element, we can create completely custom tooltips<\/a> through CSS.<\/p>\n\n\n If you want to distinguish one nav item from the next, your options are limited: You can add a border (boring) or you can add extra markup between each item (yuck). Pseudo-elements let you use any<\/em> content as a spacer between your items:<\/p>\n\n\n\n Leveraging browsers\u2019 automatic insertion of\u00a0 Using a single HTML element (plus pseudos) for each character, Yusuke Sugomori<\/a> built an entire font via CSS called CSS Sans<\/a>. The interactive demo lets you browse all of the clever techniques (e.g. rotation, border-radius, and skew) Yusuke used to build each character.<\/p>\n\n\n\n The font is limited to the uppercase Latin alphabet and is quite lovely. The CSS Sans project is a great demonstration of creativity built around constraints.<\/p>\n","protected":false},"excerpt":{"rendered":" It’s pretty amazing what you can do with the pseudo elements ::before<\/code> and ::after<\/code><\/a>. For every element on the page, you get two more free ones that you can do just about anything another HTML element could do. They unlock a whole lot of interesting design possibilities without negatively affecting the semantics of your markup. Here’s a whole bunch of those amazing things. A roundup, if you will.<\/p>\n\n\nGive you multiple background canvases<\/h3>\n\n\n
<\/figure>\n\n\n\nExpand the number of shapes you can make with a single element<\/h3>\n\n\n
<\/figure>\n\n\n\n#star-six {\n width: 0;\n height: 0;\n border-left: 50px solid transparent;\n border-right: 50px solid transparent;\n border-bottom: 100px solid red;\n position: relative;\n}\n#star-six:after {\n width: 0;\n height: 0;\n border-left: 50px solid transparent;\n border-right: 50px solid transparent;\n border-top: 100px solid red;\n position: absolute;\n content: \"\";\n top: 30px;\n left: -50px;\n}<\/code><\/pre>\n\n\nShow URL’s of links in printed web pages<\/h3>\n\n\n
<\/figure>\n\n\n\n@media print {\n a[href]:after {\n content: \" (\" attr(href) \") \";\n }\n}<\/code><\/pre>\n\n\nClear floats<\/h3>\n\n\n
<\/figure>\n\n\n\n.group:before,\n.group:after {\n content: \"\";\n display: table;\n}\n.group:after {\n clear: both;\n}\n.group {\n zoom: 1; \/* For IE 6\/7 (trigger hasLayout) *\/\n}<\/code><\/pre>\n\n\nSimulate float: center;<\/h3>\n\n\n
<\/figure>\n\n\n\nLabel blocks of code with the language it is in<\/h3>\n\n\n
<\/figure>\n\n\n\npre::after {\n content: attr(rel);\n position: absolute;\n top: 22px;\n right: 12px;\n}<\/code><\/pre>\n\n\nCreate an entire icon set<\/h3>\n\n\n
<\/figure>\n\n\n\nUse available space more efficiently<\/h3>\n\n\n
<\/figure>\n\n\n\nApply typographic flourishes<\/h3>\n\n\n
<\/figure>\n\n\n\nh2 {\n text-align: center; \n}\nh2:before, h2:after {\n font-family: \"Some cool font with glyphs\", serif;\n content: \"\\00d7\"; \/* Some fancy character *\/\n color: #c83f3f;\n}\nh2:before {\n margin-right: 10px;\n}\nh2:after {\n margin-left: 10px;\n}<\/code><\/pre>\n\n\nCreate full browser width bars<\/h3>\n\n\n
<\/figure>\n\n\n\nCreate a body border<\/h3>\n\n\n
<\/figure>\n\n\n\nbody::before, body::after {\n content: \"\";\n position: fixed;\n background: #900;\n left: 0;\n right: 0;\n height: 10px;\n}\nbody::before {\n top: 0;\n}\nbody::after {\n bottom: 0;\n}\nbody {\n border-left: 10px solid #900;\n border-right: 10px solid #900;\n}<\/code><\/pre>\n\n\nMake a gleaming button<\/h3>\n\n\n
<\/figure>\n\n\n\nFade out a page when a particular link is rolled over<\/h3>\n\n\n
<\/figure>\n\n\n\nStyle a header like a three dimensional ribbon<\/h3>\n\n\n
<\/figure>\n\n\n\nz-index<\/code>, which in some cases requires an additional wrapping element to prevent losing the element underneath a parent with an opaque background.<\/p>\n\n\nStyle the numbers in ordered lists<\/h3>\n\n\n
<\/figure>\n\n\n\nMake data tables responsive<\/h3>\n\n\n
<\/figure>\n\n\n\nCreate styled tooltips<\/h3>\n\n\n
<\/figure>\n\n\n\nAdd delimiters between navigation items<\/h3>\n\n\n
<\/figure>\n\n\n\n.menu li:before {\n content: \"\/\/ \";\n position: relative;\n left: -1px;\n}<\/code><\/pre>\n\n\nMake an entire website without HTML<\/h3>\n\n\n
<\/figure>\n\n\n\n<html><\/code>,\u00a0<head><\/code>\u00a0and\u00a0<body><\/code>\u00a0tags,\u00a0Mathias Bynens<\/a>\u00a0shows\u00a0how to create a website using only CSS<\/a>\u00a0and pseudo-element content.<\/p>\n\n\nBuild a CSS-only font<\/h3>\n\n\n
<\/figure>\n\n\n\n:before<\/code> and :after<\/code>. For every element on the page, you get two more free ones that you can do just about anything another HTML element could do. They unlock a whole lot of interesting design possibilities without negatively affecting the semantics of your markup. Here’s a whole bunch of those amazing things. A roundup, if you will.<\/p>\n","protected":false},"author":3,"featured_media":274934,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0","_share_on_mastodon_status":"%title% %permalink%"},"categories":[4],"tags":[],"class_list":["post-9516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles"],"acf":{"show_toc":"No"},"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2011\/06\/psuedos.png?fit=1200%2C600&ssl=1","jetpack-related-posts":[{"id":151172,"url":"https:\/\/css-tricks.com\/custom-controls-in-html5-video-full-screen\/","url_meta":{"origin":9516,"position":0},"title":"Hiding Native HTML5 Video Controls in Full-Screen Mode","author":"Sara Soueidan","date":"September 26, 2013","format":false,"excerpt":"The following is a guest post by Sara Soueidan. I know Sara through all her excellent work on CodePen. She was working on some custom HTML5 video controls and noticed that the customizations were lost when the video went into full screen mode (example of that happening). Digging into the\u2026","rel":"","context":"In "Articles"","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":351992,"url":"https:\/\/css-tricks.com\/7-practical-uses-for-the-before-and-after-pseudo-elements-in-css\/","url_meta":{"origin":9516,"position":1},"title":"7 Practical Uses for the ::before and ::after Pseudo-Elements in CSS","author":"Habdul Hazeez","date":"September 21, 2021","format":false,"excerpt":"CSS ::before and ::after pseudo-elements allow you to insert \"content\" before and after any non-replaced element (e.g. they work on a