1

I have a website i am trying to speed up and I am doing small things here and there. One of the things I would like to do but have not ever learned how to do is utilize image sprites.

Example here: http://flag-sprites.com/

Going to use the following code as an example:

<ul>
<li><div class"flag flag-zw"></div> Country Name</li>
<li><div class"flag flag-zw"></div> Country Name</li>
<li><div class"flag flag-zw"></div> Country Name</li>
<li><div class"flag flag-zw"></div> Country Name</li>
</ul>

I am trying to get the flags to show up beside the country name. This sprite comes pre positioned so that all I have to do is place the image and call the class. Any help would be greatly appreciated, thanks!

3
  • 4
    css-tricks.com/css-sprites Commented Dec 6, 2013 at 15:59
  • 1
    Step 1. Replace the periods with spaces in your class attribute. Step 2. Read the article that @Curt linked to. Commented Dec 6, 2013 at 16:03
  • Another great tool that goes hand in hand with sprite sheets is Sprite Cow. Commented Dec 6, 2013 at 16:25

3 Answers 3

3

Here's a FIDDLE

<ul>
  <li><span class="flag flag-ad"></span>Country Name</li>
  <li><span class="flag flag-ae"></span>Country Name</li>
  <li><span class="flag flag-af"></span>Country Name</li>
  <li><span class="flag flag-ag"></span>Country Name</li>
</ul>


.flag {
  background: url(http://s22.postimg.org/u0nt2h8qp/flags.png) no-repeat;
  display: inline-block;
  margin-right: 10px;
  width: 16px;
  height: 11px;
}
.flag.flag-ad { background-position: -16px 0 }
.flag.flag-ae { background-position: -32px 0 }
.flag.flag-af { background-position: -48px 0 }
.flag.flag-ag { background-position: -64px 0 }

Fiddle result

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like the code examples already posted will get you what you need. But, in addition to that I would like to suggest the tool Sprite Cow. This tool makes finding css position values very easy.

1 Comment

Very nice! Thanks for your post! Now that I know how to use the sprites this tool makes it much easier. =)
0

Here you go:

I would make the flags span but div is fine too:

<li><span class="flag flag-aw"></span> Aruba</li>

CSS

.flag {width: 16px; height: 11px; background: url("http://static.flag-sprites.com/i/flags.png") no-repeat 0 0; margin-right: .3em; display: inline-block;}

.flag-aw {background-position: -224px 0;}

Example:

FIDDLE

..Theres no need to bother with "blank.gif" . If you have Phtoshop or GIMP(free) you can get the proper dimensions where the images are positioned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.