CSS Lists


Unordered Lists:

  • Coffee
  • Tea
  • Coca Cola
  • Coffee
  • Tea
  • Coca Cola

Ordered Lists:

  1. Coffee
  2. Tea
  3. Coca Cola
  1. Coffee
  2. Tea
  3. Coca Cola

HTML Lists and CSS List Properties

In HTML, there are primarily two types of lists:

  • Unordered lists (<ul>) - items in the list are shown with bullets.
  • Ordered lists (<ol>) - use numbers or letters to mark the items in the list.

The CSS list properties enable you to:

  • Change the symbols or numbers used for each item in a numbered list.
  • Change the symbols for each item in a bulleted list.
  • Use an image to mark the items in a list.
  • Put colors in the background of your lists and the things inside the lists.

Different List Item Markers

The list-style-type property tells us what kind of marker is used for list items.

Here's an example that displays various markers for list items:

Example

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;
}
Try it Yourself »

An Image as The List Item Marker

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

Example

ul {
  list-style-image: url('sqpurple.gif');
}
Try it Yourself »

Position The List Item Markers

The list-style-position property tells us where the little bullets or markers in a list should be placed.

"list-style-position: outside;" means that the bullet points will be placed outside the list item. This makes sure that the beginning of each line in a list item lines up vertically. This is how it normally works by default.

  • Coffee - A brewed drink prepared from roasted coffee beans...
  • Tea
  • Coca-cola

"list-style-position: inside;" means that the little dots or dashes used for bullet points in a list will be positioned within the list item itself. This makes them part of the text and makes the text start right after them.

  • Coffee - A brewed drink prepared from roasted coffee beans...
  • Tea
  • Coca-cola

Example

ul.a {
  list-style-position: outside;
}

ul.b {
  list-style-position: inside;
}
Try it Yourself »

Remove Default Settings

You can use the list-style-type:none property to get rid of those little markers or bullets in a list. It's important to know that lists also come with some default space around them. To get rid of that space, you can simply add margin:0 and padding:0 to your <ul> or <ol> elements.

Example

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
Try it Yourself »

List - Shorthand property

The list-style property is like a shortcut for setting various properties of a list all at once. It lets you change how lists look in one go.

Example

ul {
  list-style: square inside url("sqpurple.gif");
}
Try it Yourself »

When you use the shorthand property, remember that the order of the property values matters:

  • list-style-type determines the type of bullet or marker used in a list. If a specific image is chosen for the bullet but can't be shown for some reason, this property sets the fallback style for the bullet.
  • The list-style-position attribute determines if the markers for list items should be inside or outside the main content.

  • Styling List With Colors

    We can use colors to make lists look more appealing.

    When you add something to the <ol> or <ul> tag, it affects the whole list. But if you add properties to the <li> tag, it only affects each list item.

    Example

    ol {
      background: #ff9999;
      padding: 20px;
    }

    ul {
      background: #3399ff;
      padding: 20px;
    }

    ol li {
      background: #ffe5e5;
      color: darkred;
      padding: 5px;
      margin-left: 35px;
    }

    ul li {
      background: #cce5ff;
      color: darkblue;
      margin: 5px;
    }

    Result:

    1. Coffee
    2. Tea
    3. Coca Cola
    • Coffee
    • Tea
    • Coca Cola
    Try it Yourself »
  • list-style-image lets you choose an image to represent each item in a list.

If any of the values for these properties is not provided, the system will automatically use a default value in its place.