CSS

HTML Lists and CSS List Properties

In HTML, there are two types of lists:

  • unordered lists (<ul>) - the list items are marked with bullets
  • ordered lists (<ol>) - the list items are marked with numbers or letters
The CSS list properties allow you to:
  • Set different list item markers for ordered lists
  • Set different list item markers for unordered lists
  • Set an image as the list item marker
With CSS, lists can be styled further, and images can be used as the list item marker.

Different List Item Markers

The list-style-type property specifies the type of list item marker.

Example:


<!DOCTYPE html>
<html>
<head>
<style>
ul.a {
    list-style-type: circle;
}

ul.b {
    list-style-type: square;
}

ol.c {
    list-style-type: upper-roman;
}

ol.d {
    list-style-type: lower-alpha;
}
</style>
</head>
<body>

<p>Example of unordered lists:</p>
<ul class="a">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>

<ul class="b">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>

<p>Example of ordered lists:</p>
<ol class="c">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ol>

<ol class="d">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ol>

</body>
</html>

An Image as The List Item Marker

The list-style-image property specifies an image as the list item marker:

Example:


<!DOCTYPE html>
<html>
<head>
<style>
ul {
    list-style-image: url('sqpurple.gif');
}
</style>
</head>
<body>

<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>

</body>
</html>

Position The List Item Markers

The list-style-position property specifies whether the list-item markers should appear inside or outside the content flow:

Example:


<!DOCTYPE html>
<html>
<head>
<style>
ul.a {list-style-position:inside;}
ul.b {list-style-position:outside;}
</style>
</head>

<body>
<p>The following list has list-style-position: inside:</p>
<ul class="a">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>

<p>The following list has list-style-position: outside:</p>
<ul class="b">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>

<p>"list-style-position: outside" is the default setting.</p>
</body>
</html>

List - Shorthand property

The list-style property is a shorthand property. It is used to set all the list properties in one declaration
When using the shorthand property, the order of the property values are:

  1. list-style-type (if a list-style-image is specified, the value of this property will be displayed if the image for some reason cannot be displayed)
  2. list-style-position (specifies whether the list-item markers should appear inside or outside the content flow)
  3. list-style-image (specifies an image as the list item marker)
If one of the property values above are missing, the default value for the missing property will be inserted, if any.

Example:


<!DOCTYPE html>
<html>
<head>
<style>
ul {
    list-style: square inside url("sqpurple.gif");
}
</style>
</head>
<body>

<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>

</body>
</html>

Styling List With Colors

We can also style lists with colors, to make them look a little more interesting.
Anything added to the <ol> or <ul> tag, affects the entire list, while properties added to the

  • tag will affect the individual list items:

    Example:

    
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    ol {
        background: #ff9999;
        padding: 20px;
    }
    
    ul {
        background: #3399ff;
        padding: 20px;
    }
    
    ol li {
        background: #ffe5e5;
        padding: 5px;
        margin-left: 35px;
    }
    
    ul li {
        background: #cce5ff;
        margin: 5px;
    }
    </style>
    </head>
    <body>
    
    <h1>Styling Lists With Colors:</h1>
    
    <ol>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Coca Cola</li>
    </ol>
    
    <ul>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Coca Cola</li>
    </ul>
    
    </body>
    </html>
    
    

    Result :

    Example:

    
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    ul {
        list-style-type: none;
        padding: 0;
        border: 1px solid #ddd;
    }
    
    ul li {
        padding: 8px 16px;
        border-bottom: 1px solid #ddd;
    }
    ul li:last-child {
        border-bottom: none
    }
    
    </style>
    </head>
    <body>
    
    <p>Full-width bordered list:</p>
    <ul>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Coca Cola</li>
    </ul>
    
    </body>
    </html>
    
    

    Result :

    Different Styles :

    ul.a {list-style-type: circle;}
    ul.b {list-style-type: disc;}
    ul.c {list-style-type: square;}
    
    ol.d {list-style-type: decimal;}
    ol.d {list-style-type: decimal-leading-zero;}
    ol.e {list-style-type: lower-alpha;}
    ol.f {list-style-type: lower-roman;}
    ol.g {list-style-type: upper-alpha;}
    ol.h {list-style-type: upper-roman;}