Menu Search
Jump to the content X X
Smashing Conf San Francisco

We use ad-blockers as well, you know. We gotta keep those servers running though. Did you know that we publish useful books and run friendly conferences — crafted for pros like yourself? E.g. upcoming SmashingConf San Francisco, dedicated to smart front-end techniques and design patterns.

Styling Elements With Glyphs, Sprites and Pseudo-Elements

In 2002, Mark Newhouse published the article "Taming Lists1", a very interesting piece in which he explained how to create custom list markers using pseudo-elements. Almost a decade later, Nicolas Gallagher came up with the technique pseudo background-crop2 which uses pseudo-elements with a sprite.

Today, on the shoulders of giants, we’ll try to push the envelope. We’ll discuss how you can style elements with no extra markup and using a bidi-friendly high-contrast proof CSS sprite3 technique. The technique will work in Internet Explorer 6/7 as well.

Displaying icons in links and as custom list markers

Starting with special characters Link

There is a plethora of glyphs4 out there that we could use instead of images to create custom markers. This should improve:

  • performance (there is no HTTP request)
  • usability (these characters will grow or shrink according to user’s settings)
  • maintenance (no sprite to create, no asset to deal with)
  • accessibility (see further below).

Example: Link

The markers (♠, ♣, ♥, ♦) in the list above are created via the following rules:


<ul class="glyphs"> 
	<li class="one">performance</li>
	<li class="two">usability</li> 
	<li class="three red">maintenance </li> 
	<li class="four red">accessibility</li> 


.glyphs {
  list-style-type: none;

.glyphs li:before,
.glyphs b {
  display: inline-block;
  width: 1.5em;
  font-size: 1.5em;
  text-align: center;

.one {
        background-image: expression(this.runtimeStyle.backgroundImage="none",this.innerHTML = ''+this.innerHTML);
.one:before {
        content: "2660"; /* ♠ */
.two {
        background-image: expression(this.runtimeStyle.backgroundImage="none",this.innerHTML = ''+this.innerHTML);
.two:before {
        content: "2663"; /* ♣ */
.three {
        background-image: expression(this.runtimeStyle.backgroundImage="none",this.innerHTML = ''+this.innerHTML);
.three:before {
        content: "2665"; /* ♥ */
.four {
        background-image: expression(this.runtimeStyle.backgroundImage="none",this.innerHTML = ''+this.innerHTML);
.four:before {
        content: "2666"; /* ♦ */

.red b,
.red:before {
  color: red;

How does this work? Link

  • The value of the content property must be an escaped reference to the hexadecimal Unicode character5 value (for IE, we use HTML entities6).
  • Internet Explorer 6/7 do not support ::before nor :before, so the characters are plugged via CSS expressions.
  • IE8 does not support ::before, but does support the single colon notation
  • Please notice that putting aside browser support, “there’s no difference7 between :before and ::before, or between :after and ::after. The single colon syntax (e.g. :before or :first-child) is the syntax used for both pseudo-classes and pseudo-selectors in all versions of CSS prior to CSS3. With the introduction of CSS3, in order to make a differentiation between pseudo-classes and pseudo-elements, in CSS3 all pseudo-elements must use the double-colon syntax, and all pseudo-classes must use the single-colon syntax.”
  • In IE, characters are wrapped in <b> elements, so we have a means to target and style them (you may rather want to rely on a class name for that).

Note that the CSS expressions we use here are not as bad as the ones generally used to mimic min-width8 and the like. These are only evaluated once, which should result in a small performance hit.

Displaying Images Via Pseudo-Elements Link

The main advantage of using a pseudo-element for the sole purpose of displaying an image is that it allows designers to crop a sprite. Actually, this is nothing new, and many websites are already using extra (aka "junk") markup to achieve this. For example, Yahoo! Search uses empty <s> and Facebook uses empty <i> tags for this purpose. Going this route allows for the creation of compact CSS sprites, without empty space between the images within the sprite.

The two examples below do not use extra markup and they both share the same sprite:


The two images below — which are the second icon in the sprite — are generated using each technique, respectively.

Nicolas Gallagher’s method Link

Styling the pseudo-element with a background image:
#first:before {
    content: "";
    float: left;
    width: 15px;
    height: 15px;
    margin: 4px 5px 0 0;
    background: url(sprite.png) -15px 0;

The new url() / clip method Link

Using the content property to insert the sprite which is then cropped with clip:
#second {
  position: relative;
  padding-left: 20px;
  background-image: expression(this.runtimeStyle.backgroundImage="none",this.innerHTML = '<img alt="" src="sprite.png">'+this.innerHTML);

#second img {
  content: url(sprite.png);
  position: absolute;
  top: 3px;
  clip: rect(0 30px 15px 15px);
  left: -15px; /* to offset the clip value */
  _left: -35px; /* some massaging for IE 6 */

In case you wonder why I use position: absolute in the above rule, it is because the clip property only applies to absolutely positioned elements.

The New Technique: How Does It Work? Link

  • Instead of styling the pseudo-element with a background, we use it to insert an image (via content).
  • Using the clip property, we crop this image to only display the part we want to show. It means that there is no need to add empty space in the image to avoid other parts to show as well (usually used as background image of larger elements).
  • We offset clip values by using the left and/or top properties.

With a non-cropping technique, images in sprites would have to start from the right hand side or left hand side to accommodate RTL/LTR contexts (background-position: [left]|[right] [vertical value]). Another limitation is creating sprites with images showing next to each other (because other images could be displayed as well). But when cropping sprites, these issues are not in play, so all images can be tucked together.

For an example, see figure below:

Two sprites for different contexts versus one sprite for both LTR and RTL interfaces

Advantages of this method over existing techniques Link

Styled to print
Unlike background images, these images are printed with the document (they are sent to the printer).

Styled to be accessible
Unlike background images, these images will not disappear in MS Windows’ high contrast mode9 or with high-contrast styles sheets10.

Styled to work in IE lt 8
This method works in Internet Explorer 6 and 7 as well.

Note that data URI scheme11 could be used to avoid the HTTP request. IE6/7 do not support data URI scheme, but we can use MHTML for IE6/712 to make IE7 and older browsers understand it as well.

Nicolas Gallager shows plenty of cool stuff13 one can do with pseudo-elements. The only thing I’d add here is the use of ::after to style links à la "read more" and the like, for example:

Read more14


.more:after {
        content: " 0BB"; /* » */
.more {
        background-image: expression(this.runtimeStyle.backgroundImage="none",this.innerHTML = this.innerHTML+' »');

A word about accessibility Link

You should assume that generated content is read by screen-readers15, and since there is no mechanism to offer alternative text for images plugged via the content property, we should make sure those images are purely decorative because screen-reader users would not have access to that information.

Further reading Link

You might want to take a look at the following related resources:

Credits: Icons by FatCow Web Hosting21 [CC-BY-3.0-us], via Wikimedia Commons22

dt{ font-weight: bold; }
.glyphs {list-style-type:none;}
.glyphs li:before,
.glyphs b {display:inline-block;width:1.5em;font-size:1.5em;text-align:center;}
/* advantage of grouping these: the rule will show in Firebug :) */
.one:before {
content: “2660”; /* ? */
background-image: expression(this.runtimeStyle.backgroundImage=”none”,this.innerHTML = ‘‘+this.innerHTML);
.two:before {
content: “2663”; /* ? */
background-image: expression(this.runtimeStyle.backgroundImage=”none”,this.innerHTML = ‘‘+this.innerHTML);
.three:before {
content: “2665”; /* ? */
background-image: expression(this.runtimeStyle.backgroundImage=”none”,this.innerHTML = ‘‘+this.innerHTML);
.four:before {
content: “2666”; /* ? */
background-image: expression(this.runtimeStyle.backgroundImage=”none”,this.innerHTML = ‘‘+this.innerHTML);
.red b,
.red:before {color:red;}
.four:hover:before {color:teal;}
#first:before {
margin:4px 5px 0 0;
background:url( -15px 0;
#second {
background-image: expression(this.runtimeStyle.backgroundImage=”none”,this.innerHTML = ‘‘+this.innerHTML);
#second img {
clip:rect(0 30px 15px 15px);
left:-15px; /* to offset the clip value */
_left:-35px; /* some massaging for IE 6 */
.more:after {
content: ” 0BB”; /* » */
background-image: expression(this.runtimeStyle.backgroundImage=”none”,this.innerHTML = this.innerHTML+’ »’);

Footnotes Link

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14 #
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22 ''

↑ Back to top Tweet itShare on Facebook


Thierry is passionate about Web Design and CSS. He loves the challenge of solving problems and dreaming up original ideas that get him out of bed in the morning and keep him working late at night. He is a front-end engineer at Yahoo! and owns TJK Design, and His twitter handle is @thierrykoblentz.

  1. 1

    Waoo this look great, never knew many of these techniques before :|
    Thanku for sharing this…

  2. 6

    I’m just giving it a try on a Mac right now. It’s so great people are working on inventing new techniques all the time.

  3. 7

    Nicolas Gallagher

    March 19, 2011 12:14 pm

    Very interesting article Thierry.

    Your idea of using CSS expressions will surely appeal to those who aren’t prepared, able, or allowed to serve simpler sites to IE 6/7. But it’s worth noting that inserting new elements into the DOM may lead to cases when the :first-child selector will not work as expected in IE 7.

    I experimented with clip, but the requirement for absolute positioning can be limiting. For high contrast environments, I don’t think losing a decorative icon is problematic if the associated text remains visible. But for images that are more essential or no text redundancy – e.g. logos, pagination prev/next buttons, social networking icons – another method is to use url() and negative margins (e.g.

    Thanks for sharing your great work!

    • 8

      Thierry Koblentz

      March 19, 2011 3:46 pm

      Hi Nicolas,

      Thank you for the heads up regarding “:first-child” and IE7.

      I’ve checked the page you link to, but I’m not sure about the fallback mechanism for IE 6 and IE 7. It appears that it uses negative text-indent which is an issue with images off.
      I’ve been using a slightly different technique myself [1]. The method relies on img elements which give similar results across browsers.

      Note that this is an old post. Today, I’d mix that technique with the one explained in this article, to avoid the “junk” markup.

      Thanks a lot for your feedback and the inspiration!

      (the newest version relies on position:relative instead of position:absolute)

      • 9

        Nicolas Gallagher

        March 20, 2011 3:16 am

        Yeah the fallback was an existing image-replacement technique, since IE 6/7 don’t support pseudo-elements and can’t take advantage of the NIR technique. While I don’t use CSS expressions, they could be used to add the image to the DOM in IE 6/7 (as you described above) for the purposes of IR. Means you can avoid using clip.

        • 10

          Thierry Koblentz

          March 20, 2011 9:02 am

          We could do without clip, but then we’d need to plug two elements instead of one (the img with a wrapper). This is because we need to be able to crop the sprite.

          With a background-image technique we’d need only one element (which could be a img).

          • 11

            Nicolas Gallagher

            March 20, 2011 11:01 am

            For IR, the element used to crop the sprite is already there in the DOM. It’s the same one used to crop a sprite inserted using a pseudo-element and content:url(). For decorative icons that sit alongside text, I don’t think it is worth the trouble of showing them in print / HC because they aren’t necessary to understand or use the site…so background-image does the trick.

  4. 12

    This is great, although falling back on expression() for IE is a bit dangerous if you mind about performance… You might want to take a look at this:

    • 13

      Thierry Koblentz

      March 19, 2011 3:08 pm

      Hi Charles,
      As I mention in the article, this is a different type of CSS expression than the ones you refer to. These are not as bad since they are evaluated only once. If it was not the case, then you’d see more than a single icon/glyph per item…
      Thanks for joining the discussion.

  5. 14

    I love this!

  6. 15

    Awesome insight. Thanks Thierry. Pushing the envelope – way to go!

  7. 16

    Thierry Koblentz

    March 19, 2011 2:59 pm

    @Aamir, @Jakub, @Sereal, @Sascha – Thanks for the kind words/feedback

  8. 17

    I’m not getting this. It seems like you’re saying the left value in background position is actually calculated from the right in RTL contexts. So why not align the images to the centre if that’s an issue? And I would assume the keyword ‘left’ aligns it left whatever the context.

    Regardless I don’t think RTL differences would be an issue for most people: if the page is written in a LTR language, it’s usually intended to be read that way.

    • 18

      Thierry Koblentz

      March 19, 2011 8:51 pm

      Hi Scott.

      You’re correct, the keyword left positions the background image to the left of the box, regardless of context (LTR or RTL).

      But to position the image on the other side of the box, one needs to swap things horizontally . For example, using the two sprites (LTR/RTL) given above, one would have to change the declaration from:
      background-position: left top
      background-position: right top
      For the first image in the sprite to appear in the “right” corner of the box (no pun intended).

      If the images were aligned in the center of the sprite we would need to compensate their offset from the edge of the sprite with negative values to bring them close to the edge of the box. This may allow the use of a single sprite, but at the same time it would seriously increase the complexity of the background-position declarations. This is because each image inside the sprite would need custom negative values (to accommodate both LTR/RTL contexts).

      And yes, you’re right saying that most people do not have to care for both contexts.

      Thanks for your feedback!

  9. 19

    Dear Smashing Magazine team , why you don’t do some video tutorials from time to time !!
    i believe its more details than text tutorials .
    thanks .

    • 20

      Smashing Media Customer Support

      March 20, 2011 3:08 am

      Thank you very much for your suggestion. We consider every single suggestion we receive, and we are constantly looking for advice for improvement and helpful tips from our readers.

      • 21

        Yes, that’s right. Provide video contents but please also put text tutorials as alternate content.

    • 22

      Michel Bozgounov

      March 21, 2011 1:04 am

      Video tutorials are hard to follow sometimes. You can’t easily read them in an office, for example, since you’ll have to be with headphones.

      Reading texts is much easier.

      Also, when there is code involved, much better is to follow text and code examples. This would not work if it was a video… :)

      @Smashing Mag:

      If you make video tutorials, please make sure there is also a plain text/images version available! :)

  10. 23

    Does anyone know where I can find those little 16x16px looking browser icons used in the first image figure?

  11. 26

    Gareth Partridge

    March 20, 2011 5:18 am

    Great stuff, lots to play around with here, thank you for sharing.

  12. 27

    Very informative and up to date. Loved it and definitely gonna consider it. Cheers

  13. 28

    im not much of a developer, but I really like the visuals these little additions can give to a website. I look forward to sharing this article with my team :)


  14. 29

    Jaana Gilbert

    March 20, 2011 12:03 pm

    Long time no hear Thierry!!!
    Awesome to see someone from the Drumbeat past becoming famous :)
    Loved the article, gives me lots of ideas for current project.

    Take care and keep up the great work :)

    from Drumbeat 2000 past :)

    • 30

      Thierry Koblentz

      March 20, 2011 1:49 pm

      Hey Jaana,

      Drumbeat 2000! Wow!
      Those were the days, isn’t? :)

      Hope all is well on your end. Thanks for stopping by! :)

  15. 31

    Hey, nice article!! Short but sweet and something to learn!!

    I use the background sprites a lot but as you mentioned, I had to stick to either LtoR or RtoL! I’m sure gonna try the clip method!!

    Also, the special characters! I’ve known them before! Thanks ! ♠, ♣, ♥, ♦

  16. 32

    Michel Bozgounov

    March 21, 2011 1:07 am

    Excellent article!

    Not too long, not too short, and describing a clever CSS/CSS3 technique! My thanks to the author — you sure gave me some food for thought and I’ll be experimenting with some of these ideas very shortly, maybe even in my next project! ;)

  17. 34

    Hy guys,

    have u ever tried to test the code of this article in IE<9.
    it's funny, because of the expression in the css part friezes the ie.

    the expression executes every time an events is fired.

    found in the part "Starting with special characters".

    • 35

      Thierry Koblentz

      March 21, 2011 9:37 am


      You are talking about IE<9, but as far as I know IE8 does not support CSS expressions (dynamics properties). See:

      Also, how do you test your pages? Using IETester, compatibility mode in IE8 or 9 etc. or the real thing? I have a test page that IE6 in VPC loads just fine. It contains over 1,000 items styled with these CSS expressions. You can check it here:

      the expression executes every time an events is fired.

      Don’t you think if it was the case there would be more than one element plugged in each node?

      Thanks for joining the discussion.

      • 36

        Tiago Donatti

        April 1, 2011 8:48 am

        I’m having the same problem with IE7 (IE9 compatibility mode). It just runs the expressions hundreds of times and freeze.
        Any tips to avoid the multiple executions?

  18. 37

    Nice article! I just tested it and as it turned out IE 8 (Win xp) needs the wrapper element to be block level. So apply display: block on links etc. Otherwise the :before-Element will be displayed behind the wrapper element.

    • 38

      Thierry Koblentz

      March 21, 2011 9:39 am

      Hi Bernd,

      I cannot reproduce your issue, do you have a test-case I could look at?

      Thanks for your feedback

      • 39

        I’ve send you a test case to your mail address with a example HTML5 document and a screenshot.

        • 40

          Thierry Koblentz

          March 21, 2011 3:59 pm

          Hi Bernd,

          I’ve checked your document. It uses a very different construct – you are using anchors inside paragraphs so the normal implementation would be to style the paragraphs instead of the links which would solve the problem right there ;-)

          In any case, your page shows that IE8 paints the background of your anchors over the image (that’s a bug). To fix this you may want to use “display:inline-block” instead of “display:block” so the links behave more as you’d expect.

          Thanks for sending me your test case!

          • 41

            Well, but isn’t it better to make icons clickable and being able to have multiple buttons (with icons) in one paragraph or div? I think inline-block should be fine for that. Thanks and let me say it again: Nice article!

  19. 42

    Jorgen Kesseler

    March 21, 2011 5:50 am

    I really like how this technique sovles the issues that come with traditional background images but I really hate IE’s CSS expressions and avoid them like like the plague.

  20. 43

    The idea of running any production code that uses -ms code fills me with dread. I think it is better in all circumstances to avoid those kinds of solutions as they’re targetting 1 particular problem, although yes I concede the fact it works across all IE’s is a positive.

    It’s a good experiment for sure.


↑ Back to top