All the world’s flags in a sprite

For the short version: check out http://github.com/lafeber/world-flags-sprite

Lukas and I are working on a pet project when we’re traveling between Utrecht and Amsterdam. We’ve built a website that contains cheeses of many countries. I’ve been a fan of the famfamfam world flags for years, so it made sense to include them in the site.

You quickly get too many server requests when you have many separate images on one page. This is bad. So I thought of making a sprite of all the world’s flags. I’ve tried spriteme first but it didn’t work - my guess is there were too many images. Then I tried smartsprites which did the trick.

You run the command

./smartsprites.sh --root-dir-path ~/path_to_famfamfam/flags_iso/16

which generates a super long image and a new css file that looks like this:


.ad {
background-color:transparent;
background-repeat:no-repeat;
background-image: url('flags16.png');
background-position: left -336px;
}
.ae {
background-color:transparent;
background-repeat:no-repeat;
background-image: url('flags16.png');
background-position: left -352px;
}
.af {
background-color:transparent;
background-repeat:no-repeat;
background-image: url('flags16.png');
background-position: left -368px;
}
...

You’ll immediately notice a lot of duplication, which is unacceptable since there are more than 240 flags. I’ve created a simple ruby script to generate the css needed for the sprite. When run in the image directory, it generates a css file that looks like this:


.f16 .flag{background:url(flags16.png) no-repeat}
.f16 .ad{background-position:left -336px;}
.f16 .ae{background-position:left -352px;}
.f16 .af{background-position:left -368px;}
...

An example of how you can use it in your site:

<link rel="stylesheet" type="text/css" href="http://cloud.github.com/downloads/lafeber/world-flags-sprite/flags32.css" />


<ul class="f16">
<li class="flag ar">Argentina</li>
<li class="flag au">Australia</li>
<li class="flag at">Austria</li>
...
</ul>

See http://www.cheesewiki.com/ (which lists all the worlds’ cheeses) for an example.

Note that you can’t use this in a https environment, and that images won’t show if github is down. However, if people have visited a site that uses the same css file, they won’t have to download it again which speeds up things drastically!

Posted March 15th, 2010 by Martijn Lafeber
 

Comments

 
  1. Long png have more overhead than wide ones, you should try packing your flags horizontally and compare file size.

    Posted March 17th, 2010 15:04 by gloudarea
  1. Thanks, I didn’t know that. I’ll definitely try it out!

    Posted March 17th, 2010 15:17 by Martijn Lafeber
  1. hi, you have a tiny bug in your code, the list items have the wrong css class. cool tho :D

    Posted March 17th, 2010 21:40 by shawn
  1. @shawn, fixed it, thanks for mentioning it.

    Posted March 18th, 2010 15:21 by Martijn Lafeber
  1. Nice, I’ve been working on that same subject (flag sprite), and you’ve just save me a lote of work (I was just about to use photoshop and some long hard time…. brrr)

    Thanks for sharing, keep up the good work!

    Posted May 26th, 2010 15:48 by Zuul @ MagicBrain
  1. Despite the awesomeness of the sprite itself you lack including your ruby file and paying attention for making a correct example: Australia is not Austria. B-

    Posted June 9th, 2010 23:50 by Peter
  1. @Peter: This wasn’t a solution particular for Ruby. It’s better not to download the css and sprite image, but use the github link instead (for caching purposes). You can still view the files at github if you like.
    I’ve switched at and au, thanks.

    Posted June 10th, 2010 08:37 by Martijn Lafeber
  1. Super! Thanks. I have to try this out. However, I don’t think I know the 2 letter codes to all the countries. It would be great if you could add comments at the end of each line in the css files listing the country names, UTF-8 character compliant, of course. Thanks again!

    Posted September 13th, 2010 03:43 by Richard
  1. @Richard The countries corresponding to the codes can be found here: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

    Posted December 16th, 2010 11:09 by Martijn Lafeber