---
title: How to Create Beautiful and Elegant HTML Lists Using CSS
date: 2008-02-24T20:40:01+00:00
modified: 2012-06-04T15:21:43+00:00
image:: https://kaspars.net/wp-content/uploads/2008/02/bullet-image-b1.png
permalink: https://kaspars.net/blog/how-to-create-beautiful-and-elegant-html-lists-using-css
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
post_tag:
  - CSS
  - Snippet
category:
  - Development
---

# How to Create Beautiful and Elegant HTML Lists Using CSS

[![](https://kaspars.net/wp-content/uploads/2008/02/anatomy-of-html-lists-unordered-ordered.png?strip=all&quality=90&resize=500,290 "Anatomy of HTML lists, unordered, ordered")](https://kaspars.net/wp-content/uploads/2008/02/anatomy-of-html-lists-unordered-ordered.png)

HTML list have become one of the most used HTML elements for marking-up various semantic content structures — navigation, comments and even image galleries.

This article will explain and show you how to style lists inside blog posts, articles or other basic HTML documents.

Before we start, it is necessary to understand the importance of using specific HTML tags `<ul>` and `<ol>`, instead of simple numbering (like 1., 2. or •, ») for building lists. By applying content a semantic structure, we emphasize the relationships between different content elements. In case of lists we are able to imply that there is a *certain relationship* between all of the list members, which is possibly described by the paragraph introducing the list. It also helps screen reader users for whom the total number of items is announced before the rest of the list.

### Default list rendering in standards aware browsers and Internet Explorer

Let’s look at the default rendering of ordered `<ol>` and unordered `<ul>` lists by Web standards aware browsers (with Gecko, Webkit or Opera rendering engine) and Internet Explorer (IE).

[![](https://kaspars.net/wp-content/uploads/2008/02/list-rendering-standards-ie-thumb.png?strip=all&quality=90&resize=400,186 "HTML list rendering. standards vs internet explorer")](https://kaspars.net/wp-content/uploads/2008/02/list-rendering-standards-ie-thumb.png)



It turns out that IE applies default left side **margin** to the list container (`<ul>` and `<ol>`) while standards aware browsers apply left side **padding**. These differences in list rendering force us to set both padding and margin of `<ul>` to 0 and continue to work only with styling `<li>` tag.

Another thing we notice is that list bullets or numbering becomes invisible in IE with the left side margin set to 0.

### Getting the list rendering consistent among all browsers

To solve the *invisible bullet* problem described above, its a good idea to use relative positioning of list containers `<ul>` and `<ol>`. By doing so, we will be able to create much more advanced list style later without repeating most of the CSS.

#### CSS for simple lists

```
ul, ol {
   margin:auto -3em 1em 0;
   padding:0;
   position:relative;
   left:-3em;
   overflow:hidden;
}

li {
   margin-top:0.25em;
   margin-bottom:0.25em;
}

ul ul, ul ol,
ol ol, ol ul {
   margin-left:1em;
   padding-left:0;
}

ul li, ol li {
   margin-left:5em;
}

li li {
   margin-left:1em;
}
```

#### Internet Explorer specific CSS

To fix IE’s ability to do the math correctly, we have to enable [hasLayout](http://www.satzansatz.de/cssd/onhavinglayout.html) property for all of our `<ul>` and `<ol>` tags. This is done by using [conditional comments](http://www.quirksmode.org/css/condcom.html "Read about conditional comments and how are they used for targeting different versions of IE"):

```
ul, ol { height:0; overflow:visible; }
ul, ol { height:1%; }
```

##### Output

![Simple unordered list](https://kaspars.net/wp-content/uploads/2008/02/list-anatomy-simple-unordered.png)

List is now rendered equally in all browsers. For illustration purposes a yellow background is applied to the list container `<ul>`, gray border shows the dimensions of `<ul>`, while list items `<li>` have gray background.

### *Flat* lists for more content per list item

Sometimes you have multiple lines of content per list item and then it might be reasonable to align the lists with the rest of the content in order to sustain the vertical flow of it.

#### CSS for *flat* lists

```
.flat li {
   margin-left:3em;
}

.flat li ul, .flat li ol {
   margin-left:1em;
   padding-left:0;
}

.flat li li {
   margin-left:0;
}
```

![Screenshot: flat HTML lists for more content](https://kaspars.net/wp-content/uploads/2008/02/list-anatomy-flat-unordered.png)

Notice the little amount of CSS, but more importantly that it is rendered equally among all of the browsers and we can still use the default bullet styles instead of images.

However, sometimes one might want to use custom style list bullets. This can be done using the `list-style-image` property in CSS.

#### Lists with *custom style bullets*

![Screenshot: HTML lists with custom bullet image](https://kaspars.net/wp-content/uploads/2008/02/list-anatomy-unordered-custom-bullet-image.png)

*Note:* don’t forget to remove the line break after `list-style-image:` to get it working.

#### CSS:

```
ul.bullet-a li {
   list-style-image:
    url('bullet-image-a.png');
}

ul.bullet-b li {
   list-style-image:
    url('bullet-image-b.png');
}
```

Although the alignment of the list image is not pixel perfect among all of the browsers, it is more than satisfactory if the height of the bullet image doesn’t exceed 10 pixels. One might suggest to use background of `<li>` tag as a list bullet image, but this would brake the ability to combine multiple CSS identifiers per list, like `<ul class="flat bullet-a">` because of inherited margin settings.

### All the small details

Notice that the spacing between list items in the last example (rounded image bullets) is larger than the default one (arrow image bullets). This enhances readability and separates list items similarly to paragraphs. So here is the final set of CSS styles to suite most of the needs:

```
.spaced {
    margin-bottom:0;
}

.spaced ul, .spaced ol {
    margin-top:1em;
}

.spaced li {
    margin-bottom:1em;
}
```

```
.indent li {
    padding-left:1em;
    text-indent:-1em;
}

.inside li {
    list-style-position:inside;
}

.clear li {
    list-style-type:none;
}
```

You can see that one of the previous examples already utilizes the “spaced” styling, while “indent”, “inside” and “clear” might benefit from a few examples:

![Screenshot: Flat and indented list, clear list, inside list](https://kaspars.net/wp-content/uploads/2008/02/indent-flat-clear-inside-html-lists.png)

### All of CSS combined &amp; list style cheat-sheet

- [View all of the list style examples](https://kaspars.net/wp-content/uploads/2008/02/index.html) in your browser.
- View the resulting CSS as [a seperate file](https://kaspars.net/wp-content/uploads/2008/02/konstruktors-lists.css "CSS for beautiful HTML lists") (bullet images: [![Sample bullet image a](https://kaspars.net/wp-content/uploads/2008/02/bullet-image-a.png)](https://kaspars.net/wp-content/uploads/2008/02/bullet-image-a.png "Sample bullet image a") [![Sample bullet image b](https://kaspars.net/wp-content/uploads/2008/02/bullet-image-b.png)](https://kaspars.net/wp-content/uploads/2008/02/bullet-image-b.png "Sample bullet image b"))
- [ Download a list style CSS classname cheat-sheet](https://kaspars.net/wp-content/uploads/2008/02/konstruktors-html-list-cheat-sheet.pdf "HTML list style CSS cheat sheet") (.pdf) which you can print and use as a reference when composing your articles, blog posts or other HTML documents.

### Screenshot showing the use of various HTML list styles

[![List output example](https://kaspars.net/wp-content/uploads/2008/02/list-example-result-thumb.png)](https://kaspars.net/wp-content/uploads/2008/02/list-example-result.png)

#### Internet Explorer specific HTML and CSS

```
ul, ol { height:0; overflow:visible; }
ul, ol { height:1%; }
```

### Few suggestions:

- Bullet image height preferably should not exceed 10 pixels. Lower image height makes them look better in Internet Explorer. If you want bullet images to be transparent, save them as transparent GIF or 8-bit PNG files.
- You might find it useful to apply this styling only to your post content, which can be easily done by prepeding the identifier of your post wrapper (like `.post` or `.article`) to all of the list styles. The end result would be something like `.post ul, .post ol {}`.