A Whole Bunch of Amazing Stuff Pseudo Elements Can Do

Avatar of Chris Coyier
Chris Coyier on (Updated on )

It’s pretty amazing what you can do with the pseudo-elements ::before and ::after. 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.

Give you multiple background canvases

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 lots of applications of this, including multiple borders, simulating CSS3 multiple backgrounds, and equal height columns.

Expand the number of shapes you can make with a single element

All of the shapes above and many more 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.

Here’s an example of a six-pointed star:

#star-six {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
  position: relative;
}
#star-six:after {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid red;
  position: absolute;
  content: "";
  top: 30px;
  left: -50px;
}
@media print {
  a[href]:after {
    content: " (" attr(href) ") ";
  }
}

Clear floats

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”, and more semantically titled with the class name “group”.

.group:before,
.group:after {
  content: "";
  display: table;
}
.group:after {
  clear: both;
}
.group {
  zoom: 1; /* For IE 6/7 (trigger hasLayout) */
}

Simulate float: center;

The float 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.

Label blocks of code with the language it is in

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:

pre::after {
  content: attr(rel);
  position: absolute;
  top: 22px;
  right: 12px;
}

Create an entire icon set

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.

Use available space more efficiently

What CSS giveth, CSS can taketh away. What I mean is that pseudo-element content can be applied or removed conditionally via media queries. Perhaps you apply an icon when space is limited, and replace that with a more descriptive word when there is more room.

Apply typographic flourishes

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.

h2 {
  text-align: center;     
}
h2:before, h2:after {
  font-family: "Some cool font with glyphs", serif;
  content: "\00d7";  /* Some fancy character */
  color: #c83f3f;
}
h2:before {
  margin-right: 10px;
}
h2:after {
  margin-left: 10px;
}

Create full browser width bars

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 by limiting the content width with one element and making the header bar reach out to the edges with pseudo-elements.

Create a body border

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 without any dedicated markup at all.

body::before, body::after {
  content: "";
  position: fixed;
  background: #900;
  left: 0;
  right: 0;
  height: 10px;
}
body::before {
  top: 0;
}
body::after {
  bottom: 0;
}
body {
  border-left: 10px solid #900;
  border-right: 10px solid #900;
}

Make a gleaming button

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+.

Original by Anton Trollbäck; Pseudoelementized by Nicolas Gallagher; Another version by me.

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 on a link.

Style a header like a three dimensional ribbon

Everybody loves ribbons! Check out this snippet for the HTML and CSS for this one. It makes use of negative z-index, which in some cases requires an additional wrapping element to prevent losing the element underneath a parent with an opaque background.

Style the numbers in ordered lists

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.

Make data tables responsive

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 and reformat it to a more readable view when on small screens.

Create styled tooltips

Using an HTML5 data attribute, then pulling that attribute in and styling it as a pseudo-element, we can create completely custom tooltips through CSS.

Add delimiters between navigation items

Pseudo element delimiters

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 content as a spacer between your items:

.menu li:before {
  content: "// ";
  position: relative;
  left: -1px;
}

Make an entire website without HTML

Website without HTML

Leveraging browsers’ automatic insertion of <html><head> and <body> tags, Mathias Bynens shows how to create a website using only CSS and pseudo-element content.

Build a CSS-only font

CSS Sans

Using a single HTML element (plus pseudos) for each character, Yusuke Sugomori built an entire font via CSS called CSS Sans. The interactive demo lets you browse all of the clever techniques (e.g. rotation, border-radius, and skew) Yusuke used to build each character.

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.