Jan

26th

Different coloured bullet points in a CSS list

posted on Monday 26th January, 2009 by Neil

Whilst browsing round the interweb I realised that there are no really clean ways of delivering different coloured bullets to the text unless you want to use SPANs or create your own bullet images in JPG, GIF or PNG. Lets take a look at both these methods:

wrap your LI text in a SPAN

A very useful method but requires all of the text within the LI to be wrapped in a SPAN tag. This solution is not ideal, especially if you're using a CMS with a WYSIWYG interface; you'd have to tell your clients "When creating a list, please add these special tags round your list content". Not a great solution.

Use image bullet points

This works well but theres one flaw: Lets say I want to change some of my elements to another colour or need more than one colour for the bullets? Sure we could just create each bullet again using the right colour, but wouldn't it be nice if we could simply change a few lines of CSS?

Break out your HTML/CSS editor of choice and join me on a journey of discovery and wonderment!!! Whilst I'm sure that this technique is not unique, I found no reference to it on my travels through Googleland. It's actually elegantly simple.

1. Create a new bullet image using either a GIF or PNG.

This is required as we're actually making an image with a bullet sized hole in it rather than making an image of a coloured bullet. We're effectively making a "mask" so that the "hole" can show some of the content underneath. Make the image quite wide, say 50px. Take a look at the GIF below for an example. This is a bullet GIF designed for a white background, and could be changed to suit any other solid background. We just left it white so you could see it:

white bullet mask

2. Create your list in HTML


<ul>
  <li>list item one</li>
  <li>list item two</li>
  <li>list item three</li>
  <li>list item four</li>
  <li>list item five</li>
</ul>

3. Now add some CSS to our HTML HEAD

First we'll reset margin and padding on UL and LI elements:

ul, li {  
margin:0; 
padding:0;
}

Then we'll add a left border to our UL for the colour we want our bullets to be displayed in:

ul { 
border-left:30px solid #00CC33 ;
}

Great! Now lets style the remainder of the LI

ul li {
list-style-type:none;
background:#FFFFFF;
background:url(bullet.gif) left center no-repeat;
padding-left:30px;
margin-left:-30px; 
}

Notice that the ULs border should match the UL LI padding and negative margin. the negative margin simply makes the child LIs sit on top of the border we applied to the UL, and because we used a background image on the LI using our masked bullet, we can see the colour show through from underneath. All we have to do to change the colour of the "bullet" is change the colour of the UL border. Job done!!!

Heres a couple of examples of the working list.

  • list item one
  • list item two
  • list item three
  • list item four
  • list item five
  • list item one
  • list item two
  • list item three
  • list item four
  • list item five

Issues

There are (obviously) some limitations of this method:

  1. You can only use it in pages/areas where you have a solid background colour as you have to create a mask bullet using that colour. Also you would still have to have a different bullet mask for each background you were wanting to use this method on.
  2. You lose some functionality with the LI backgrounds as you've already used them.

latest news

rss iconWhat we're saying on the blog

Plesk 10

posted on Thursday 26th January, 2012 by Neil

We've recently rolled out our new control panel to all our users, Plesk 10 which is a direct replacement from Plesk 9 that we were currently using. Users will be presented with a brand new inter...

more>>

Opencart ecommerce support and hosting

posted on Tuesday 14th June, 2011 by Neil

We're now offering ecommerce solutions through Opencart, an open source ecommerce platform rich in additional features. Opencart has been around for a few years now and is gaining ground in the ...

more>>

The Internet Explorer 6 countdown

posted on Friday 1st April, 2011 by Neil

Finally Microsoft have decided to visibly let people know of the impending shutdown of Internet Explorer 6. Its a bold move but one that many developers welcome. However there are still many corpor...

more>>

Christmas season

posted on Thursday 16th December, 2010 by Neil

With Christmas well and truly here (well almost), we're preparing ourselves for extended noshing of lovely food, the exchange of presents and the pleasure of good company. I've already added a coup...

more>>

When will Internet Explorer 6 finally die?

posted on Wednesday 15th December, 2010 by Neil

It will be a bitter sweet day indeed when the staple browser for many Windows XP fans out there finally pops its clogs. It's the browser that all of us developers love to hate, with its quirky hand...

more>>
loading icon Loading... Please wait.