Home

How to Use Images in Lists for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:16:39
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

It's not quite a background, but you can also use images for list items for your HTML5 and CSS3 web page. Sometimes, you might want some type of special bullet for your lists.

image0.jpg

On this page, several varieties of peppers have been listed. For this kind of list, a custom pepper bullet is just the thing. Of course, CSS is the answer:

<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 <title>listImages.html</title>
 <style type = "text/css">
  li {
  list-style-image: url("pepper.gif");
  }
 </style>
 </head>
 <body>
 <h1>My Favorite Peppers</h1>
 <ul>
  <li>Green</li>
  <li>Habenero</li>
  <li>Arrivivi Gusano</li>
 </ul>
 </body>
</html>

The list-style-image attribute allows you to attach an image to a list item. To create custom bullets:

  1. Begin with a custom image.

    Bullet images should be small, so you may have to make something little. The image will be trimmed to an appropriate width, but it will have all the height of the original image, so make it small.

  2. Specify the list-style-image with a url attribute.

    You can set the image as the list-style-image, and all the bullets will be replaced with that image.

  3. Test the list in your browser.

    Be sure everything is working correctly. Check to see that the browser can find the image, that the size is right, and that everything looks like you expect.

If you don't want to use an image, CSS has a number of other styles you can apply to your list items. Use the list-style-type rule to set your list to one of many styles. Look at official CSS documentation for a complete list, but the most commonly used style types are disc, circle, square, decimal, upper-roman, lower-roman, upper-latin, and lower-latin.

Note that you can apply a numeric styling to a list item in an ordered or unordered list, so the distinction between these list types is less important than it used to be.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].