I am a designer, a blogger and a Wordpress nut!

Subscribe to my Feed

Instead of Using "list-style-image" Property

list-style-image

In this article I want to share my opinion on using the property “list-style-image” to place an image next to a list. I found that it is difficult to position/align the image to the proper place relative to the list item text. But then what do we use? Hang on and I will show you.

So here we have a vertical unordered list of items you want to display and you want to accompany the list items with a pretty little icon graphic next to them to highlight some points but it is a huge struggle to position the icon center aligned with the text item next to it. Have a look at the following screenshots of what the default “list-style-image” would look like next to the item list in different browsers. Use the baseline drawn as a reference.

list-style-image

Looking at the screenshot above what if I want to align the icon down a few notches so that the list item text is at the center of the icon? Well I found it very difficult to control the alignment correctly where it will play nice across all the browsers using the property “list-style-image”. Ok so what do we use? It turns out that using the “background-image” property and the “background-position” property gets the job done nice and easy and it is cross browser consistent. Let me show you how.

Have a look at the markup and css:

HTML:

        <ul id="background-image">
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
            <li>Item 4</li>
        </ul>

CSS:

ul#background-image {
	margin-top:25px; /* just to give it some spacing from the top */
	list-style:none;
}
ul#background-image li {
	background:url(../images/padlock.png) no-repeat;
	background-position:5px 0px; /* to move the icon into position of your choice */
	margin-bottom:20px; /* spacing between the list items */
	line-height:2.2em; /* to set the line height between the list items and to reveal the icon image or you can declare a height instead */
	padding-left:50px; /* to push the list item text over to the right revealing the icon */
}

Ok let’s have a look at the demo. I have put both list side by side for your comparison. The list on the left uses “list-style-image” property and the right uses “background-image” property.

CLICK TO VIEW DEMO

As you can see with the “background-image” property we were able to position it just right and it looks almost identical across all the above mentioned browsers. I guess the thing to take home from this is if you ever get stuck trying to align your pretty icons next to a list, think about using background-image property instead.

Enjoyed this Post? Please Share!
  • Twitter
  • Digg
  • del.icio.us
  • Facebook
  • Print

About the Author

Roy Ho is a web site design and developer. He blogs tutorials and articles on web design and development to share with the community. He has been practicing this field for 12 years now. He is passionate in the field and very eager to learn more!
This entry was posted in Articles, Basics, CSS + HTML and tagged , , , , . Bookmark the permalink.
Back to top

2 Responses to Instead of Using "list-style-image" Property

  1. This looks great! I’ve had trouble in the past with getting list images to position correctly, so I may have to try this. Thanks!

  2. [...] life a bit easier and helps to ensure the list looks pretty much the same across all browsers. Instead of Using “list-style-image” Property | Design in the Raw __________________ Jon Warner Web Pro Cafe :: The PixelForge :: Follow my Twits (or [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*