skip to content
Nikolas Barwicki - Javascript Blog Nikolas's Blog

Everything you need to know about HTML lists

/ 4 min read

Lists in HTML

Lists are a fundamental part of HTML and are used to present information in a structured and organized format on web pages. However, many developers tend to overlook the different types of lists that are available in HTML beyond the basic unordered list.

In this article, we will delve into the different types of lists in HTML, including unordered lists, ordered lists, and description lists, and examine their unique characteristics and best practices for implementation.

Ordered Lists

Ordered lists, also known as numbered lists, are used to display items in a specific order. Each item in the list is numbered, starting from 1 by default. You can change the starting number by using the ‘start’ attribute. To create an ordered list, use the <ol> tag, and each item is defined using the <li> tag.

Here’s an example of an ordered list in HTML:

<ol>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ol>

Unordered Lists

Unordered lists, also known as bulleted lists, are used to display items in no particular order. Each item in the list is preceded by a bullet point or some other symbol, which is defined using CSS. To create an unordered list, use the <ul> tag, and each item is defined using the <li> tag.

Here’s an example of an unordered list in HTML:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

Definition Lists

Definition lists are used to define terms and their corresponding definitions. To create a definition list, use the <dl> tag, and each term is defined using the <dt> tag, while each definition is defined using the <dd> tag.

Here’s an example of a definition list in HTML:

<dl>
  <dt>Term 1</dt>
  <dd>Definition 1</dd>
  <dt>Term 2</dt>
  <dd>Definition 2</dd>
  <dt>Term 3</dt>
  <dd>Definition 3</dd>
</dl>

Nesting Lists

Lists can be nested within each other, meaning you can have an ordered list within an unordered list, or vice versa. To nest a list, simply include the <ol>, <ul>, or <dl> tags within the <li> tag of another list.

Here’s an example of nested lists in HTML:

<ul>
  <li>
    Item 1
    <ol>
      <li>Subitem 1</li>
      <li>Subitem 2</li>
    </ol>
  </li>
  <li>Item 2</li>
  <li>
    Item 3
    <ul>
      <li>Subitem 1</li>
      <li>Subitem 2</li>
    </ul>
  </li>
</ul>

When and how to use <dl>, <dd> and <dt> html tags?

The HTML tags <dl>, <dd>, and <dt> are used to create description lists on a web page.

The <dl> tag defines the start of the description list, while the <dt> tag defines the term being described, and the <dd> tag defines the description of the term.

Here’s an example of how to use these tags:

<dl>
  <dt>Term 1</dt>
  <dd>Description of Term 1</dd>
  <dt>Term 2</dt>
  <dd>Description of Term 2</dd>
</dl>

In this example, ‘Term 1’ and ‘Term 2’ are the terms being defined, and ‘Description of Term 1’ and ‘Description of Term 2’ are the corresponding descriptions.

It is important to note that the <dt> tag should always be used within a <dl> tag and should come before the <dd> tag. The <dd> tag can be used multiple times for each <dt> tag to provide more than one description for a term.

What’s the difference between dl and other types of lists

The main difference between a description list (<dl>, <dt>, <dd>) and an unordered/ordered list (<ul>/<ol>, <li>) is that the former is used to present information in a specific format, where a term is followed by its description, whereas the latter is used to present a list of items in no particular order.

Here’s an example to illustrate the difference:

<!-- Unordered list example -->
<ul>
  <li>Apples</li>
  <li>Bananas</li>
  <li>Oranges</li>
</ul>

<!-- Description list example -->
<dl>
  <dt>Apple</dt>
  <dd>A round fruit with red, green or yellow skin and white flesh.</dd>
  <dt>Banana</dt>
  <dd>A long, curved fruit with yellow skin and soft, sweet flesh.</dd>
  <dt>Orange</dt>
  <dd>A round fruit with orange skin and juicy, sweet flesh.</dd>
</dl>

In the above example, the unordered list is used to present a simple list of fruits, while the description list is used to provide definitions for each fruit.

Summary

In summary, understanding the different types of lists in HTML is important for writing semantically valid code and presenting information in a structured and clear manner on a web page. While unordered lists with <ul> and <li> tags are commonly used, it’s also important not to forget about the description list with <dl>, <dd>, and <dt> tags. By using the appropriate list type for your content, you can make your web page more accessible and user-friendly for all visitors.