Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use different icon images for each list element within an unordered list?

I'm not an advanced user of CSS, but I have a decent working knowledge i suppose. I'm tryin to make an unordered list that uses different icons for each list element. I would also like the background colour of the list element to change upon hover.

Is there a way to do this through CSS, or would you just include the icon image within the list element (like below)?

<li><img src="voters.jpg"><a href="voters.html">Voters</a></li>

Using the list-style-image on the ul level makes all of the icons the same, and I haven't been able to figure out another way. Most examples I've found teach you how to use images in a list, but only if the bulleted images are the same. I'm definitely open to suggestions and improvements on the way I'm trying to do this.

thanks

<div class="content-nav">
  <ul>
    <li class="instructions"><a href="instructions.html">Instructions</a></li>
    <li class="voters"><a href="voters.html">Voters</a></li>
  </ul>
</div>

.content-nav {
    font-size:12px;
    width:160px;
    z-index:0;
}

.content-nav ul {
    padding:0 20px;
    margin:30px 0;
}

.content-nav li {
    padding:5px;
    margin:5px 5px;
}

.content-nav li a {
    color:#666;
    text-decoration:none;
}

.content-nav li.voters a {
    background:#FFF;
    color:#666;
    text-decoration:none;
    list-style-image:url(images/voters.jpg);
}

.content-nav li.voters a:hover {
    background:#0CF;
    color:#000;
}

.content-nav li.instructions a {
    background:#FFF;
    color:#666;
    text-decoration:none;
    list-style-image:url(images/voters.jpg);
}

.content-nav li.instructions a:hover {
    background:#0CF;
    color:#000;
}
like image 297
Patrick Avatar asked Apr 18 '11 18:04

Patrick


People also ask

How do I insert a picture into an unordered list?

The attribute is used with the HTML <ul> tag, with the CSS property list-style-image to add image bullets to an unordered list. The URL property sets the image source for the list item.

What elements make an unordered list?

<ul>: The Unordered List element. The <ul> HTML element represents an unordered list of items, typically rendered as a bulleted list.

Which tag should we use to group elements in an unordered list?

The ol element is used when the list is ordered and the ul element is used when the list is unordered. Definition lists ( dl ) are used to group terms with their definitions.


2 Answers

You could add background images on each list element, and use padding to push the text away from it.

<ul>
    <li class="li1">List 1</li>
    <li class="li2">List 2</li>
</ul>

.li1 {
   background:url('li1.gif') 50% 50% no-repeat no-repeat;
   padding-left: 5px;
}
.li2 {
   background:url('li2.gif') 50% 50% no-repeat no-repeat;
   padding-left: 5px;
}

Just make sure the padding-left is the same size as the image (or a bit larger if you want spacing)

like image 112
fin1te Avatar answered Sep 28 '22 00:09

fin1te


Using CSS3's :nth-child() selector, does not require additional markup on each <li> element:

Live Demo

HTML:

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

CSS:

ul li:nth-child(1) {
    list-style-image: url('http://google-maps-icons.googlecode.com/files/teal01.png');
}
ul li:nth-child(2) {
    list-style-image: url('http://google-maps-icons.googlecode.com/files/teal02.png');
}
ul li:nth-child(3) {
    list-style-image: url('http://google-maps-icons.googlecode.com/files/teal03.png');
}

Browser Support: IE9+, FF3.5+, SA3.1+, OP9.5+, CH2+

like image 37
drudge Avatar answered Sep 28 '22 02:09

drudge