Hansi Zimmrer
Hansi Zimmrer

Reputation: 259

HTML list with different images as bullets

I know it is possible to set an image instead of the HTML default bullets:

list-style-image: url(...);

But I havent't found a way how I can set different images in one and the same list. e.G.: Instead of the first bullet, img_1 is shown, instead of the next 5 bullets img_2 is displayed ... .

Upvotes: 5

Views: 9767

Answers (5)

Mark
Mark

Reputation: 6855

you have to/can use classes in your list items:

<ul>
  <li class="bar>
   <a href="#" title="foo">Foo</a>
  </li>
  <li class="foo>
   <a href="#" title="bar">Bar</a>
  </li>
</ul>

CSS for images as bullets for <li>

ul {
  list-style: none;
}
li {
 padding-left: 25px; /*adjust to your needs*/
}
li.bar {
  background: url(img_2.png) no-repeat 0 50%;
}
li.foo {
  background: url(img_1.png) no-repeat 0 50%;
}

CSS for images as bullets for <a>

ul {
  list-style: none;
}
li a {
 padding-left: 25px; /*adjust to your needs*/
 display: block;
}
li.bar a {
  background: url(img_2.png) no-repeat 0 50%;
}
li.foo a {
  background: url(img_1.png) no-repeat 0 50%;
}

Upvotes: 1

arifix
arifix

Reputation: 750

HTML-

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

CSS(doesn't work on IE 7,8)-

ul li:nth-child(1){
list-style-image:url('image1.png');
}
ul li:nth-child(2){
list-style-image:url('image2.png');
}
ul li:nth-child(3){
list-style-image:url('image3.png');
}

CSS for all Browser including IE 7,8

ul li:first-child{
list-style-image:url('image1.png');
}
ul li:first-child + li{
list-style-image:url('image2.png');
}
ul li:first-child + li + li{
list-style-image:url('image3.png');
}
ul li:first-child + li + li + li{
list-style-image:url('image4.png');
}

Upvotes: 5

Dinesh Kanivu
Dinesh Kanivu

Reputation: 2587

You need to use nth-child Property

CSS

li:nth-child(1){list-style-image:url(image)}

Here is the demo http://jsfiddle.net/5VB2u/

Upvotes: 2

cardeol
cardeol

Reputation: 2238

Just add a different class in the LI element

<ul class="navlist">
    <li class="img_1">element1</li>
    <li class="img_2">element2</li>
</ul>
<style>
.navlist li.img_1
{
padding-left: 10px;
background-image: url(images/image1.gif);
background-repeat: no-repeat;
background-position: 0 .5em;
}
.navlist li.img_2
{
padding-left: 10px;
background-image: url(images/image2.gif);
background-repeat: no-repeat;
background-position: 0 .5em;
}
</style>

Upvotes: 3

chris
chris

Reputation: 4867

I think you can do this with classes

<li class="first"></li>
<li class="second"></li>
...

li.first {
  ⋮ declarations
}

li.second {
  ⋮ declarations
}
...

or the nth-child selector

li:nth-child(1) {
  ⋮ declarations
}

li:nth-child(2) {
  ⋮ declarations
}
...

Upvotes: 1

Related Questions