January 31, 2014

31

Replacing List Bullets with Images Using CSS

by Elizabeth on January 31, 2014

by Elizabeth Kricfalusi, Tech for Luddites

This post was originally published in February 2010. It has been updated with additional instructions for different scenarios.

Replacing standard HTML list bullets with images can be a great way to tie them into your overall theme and make your site more visually appealing.

Note: When I originally wrote this article, it was as part of a series of posts I was writing about customizing the Thesis theme for WordPress websites. However, the information applies to any site where you have the ability to edit the CSS file. You don't need to be using either WordPress or Thesis.

This is what a standard bulleted list looks like on a web page:

  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines
  • List item 2
  • List item 3

The HTML code to create this list looks like the following. The <ul> stands for "unordered list" and the <li> stands for "list item." (If you want to use numbers instead of bullets, you would use <ol>, or "ordered list".)

<ul>
<li>I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>


Before showing you how to replace those bullets with images, I'll add that there are some other shapes you can use built into HTML itself. These include a circle, disc (filled circle), square, and various number formats. You can see the full list on the w3schools website.

You change the style by changing the definition for <ul> in your CSS stylesheet.

ul {
list-style-type: lower-greek;
}

Here's what the list looks like now.

  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines
  • List item 2
  • List item 3

Note: If you use the CSS definition above, it will affect every list item you have on your site. If you want to just have that style for certain lists, you'll need to create a new class to apply to the <ul>. For example, you could create a class called "lower-greek" and change your CSS to:

ul.lower-greek {
list-style-type: lower-greek;
}

Then you'd add that class into the <ul> tag in the HTML as follows:

<ul class="lower-greek">
<li>I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
Host Unlimited Domains on 1 Account

Now, to change the bullet to an image, you can add an attribute called list-style-image to your CSS definition. For this example, I'm using a leaf image that's 24 x 38 pixels. The CSS code would now be:

ul.leaf {
list-style-image: url('http://www.techforluddites.com/images/leaf_icon.jpg');
}

and the HTML would be:

<ul class="leaf">
<li>I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>

This is what the list looks like on the web page:

  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines as well.
  • List item 2
  • List item 3

Now, you can see the problem with this approach is that the bullet isn't positioned very nicely relative to the text. That's because when you add the list-style-image attribute to the <ul> tag, it uses the same positioning and spacing between the bullet and the text, regardless of the size of the image. So that might work well if your image is about the same size as a standard bullet but not so well if it's much bigger or smaller.

Another option is to eliminate the standard bullet altogether using the <ul> tag and then add a background image in the <li> tag so you can use further attributes to adjust the positioning exactly the way you want it.

So the CSS would now be:

ul.no_bullet {
list-style-type: none;
padding: 0;
margin: 0;
}

li.leaf {
background: url('http://www.techforluddites.com/images/leaf_icon.jpg') no-repeat left top;
}

and the HTML would be:

<ul class="no_bullet">
<li class="leaf">I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines</li>
<li class="leaf">List item 2</li>
<li class="leaf">List item 3</li>
</ul>

The list will then look like this:

  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines as well.
  • List item 2
  • List item 3

Now, obviously, we still need to do a little more work. The bullet image is being cut off on the bottom so we need to set a list item height value that is at least as big as the height of the image. We also need space between the image and the text, which we set using padding-left.

Note: Padding values will change where the text appears relative to the image within an individual list item. Margin values will change where the entire line—including the bullet—appears relative to other items on the page, including the next list item. You can also control the position of the bullet image using the background declaration, replacing "left" and "top" with actual pixel values. Basically you just have to start with something and then play around with the values until you have them where you want them.

I can't give you exact values because it will depend on a number of variables including the size of the bullet, how much spacing you want, font size, and so on. But here's the change I've made to the CSS and the resulting output.

li.leaf {
background: url('http://www.techforluddites.com/images/leaf_icon.jpg') no-repeat left top;
height: 34px;
padding-left: 44px;
}
  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines as well.
  • List item 2
  • List item 3

For Smaller Bullet Images

The above works very well if your image is bigger than a standard bullet. If it's smaller, you might end up with something that looks like this:

  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines as well.
  • List item 2
  • List item 3

You can see that the text from one bullet runs into the next line. Now you could fix that by specifying a larger height value again, but that will look odd for such a small bullet, e.g.

  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines as well.
  • List item 2
  • List item 3

In this instance, you'll want to add a display attribute and adjust the top positioning in the background image (replace "top" with an actual dimension). So the CSS will now look like this:

li.red_square {
background: url('http://www.techforluddites.com/images/red-square-4x4.jpg') no-repeat left 9px;
padding-left: 12px;
display: block;
}

And the output will be:

  • I'm writing a long list item 1 so you can see what happens when the text wraps across multiple lines as well.
  • List item 2
  • List item 3

Again, play with the values until it looks the way you want it. And note that you can also reposition larger bullet images as well if you don't like the way it's aligning with the text.

Note: Thanks to reader Sibley who shared the tip about needing a "display: block" attribute for smaller bullets.

Last Updated: March 28, 2014

 

Comments

{ 31 comments… read them below or add one }

Daniel March 24, 2014 at 6:05 am

I got a little bit stuck here & wondered if anyone can help. I’ve got a few of the examples above working, but I’m obviously missing something when I try & combine the custom li.

This is my css code.
ul.leaf {
list-style-type: none;
padding: 0;
margin: 0;
}
.custom li.leaf {
background: url(‘http://www.techforluddites.com/images/leaf_icon.jpg’) no-repeat left top;
height: 28px;
padding-left: 44px;
}

However, when I go into a html page & create a list using the code

I’m writing a long list item 1 so you can see what happens when the text wraps across multiple lines

I just get the standard system bullet, not the leaf. What I’m trying to produce is a list that produces the same result as the .custom li.leaf example shown above, but somewhere in my code I’ve obviously done something wrong. Any help would be appreciated.

Reply

Elizabeth March 25, 2014 at 3:44 pm

Hi Daniel.

Can you send me an email to F1@TechForLuddites.com with the URL and I may be able to help?

Thanks.

Elizabeth

Reply

thomas February 18, 2014 at 11:05 pm

This is helpful but only references the trivial solution. What if you wanted to create a customized multi-level list?

Reply

Elizabeth February 22, 2014 at 8:54 am

In that case, you would create new classes for the nested lists using the same steps as for the top-level list.

Reply

al March 13, 2014 at 4:18 am

how do you accomplish this?
im trying to nest two lists with different images, but the class from the outer list flows to the inner list, ignoring whatever class I set for it

Reply

Elizabeth March 15, 2014 at 3:03 pm

I’d have to see the site to see what’s happening. CSS is tricky with the class selectors, for sure.

Reply

Sean January 27, 2014 at 11:33 am

This one helped me out a lot! Thanks a bunch!

Reply

Leave a Comment

{ 5 trackbacks }

Previous post:

Next post: