Skip to content

HTML Lists

HTML provides several elements for creating lists, essential for grouping and structuring related items.

  • Unordered Lists (<ul>): For items where the order doesn’t matter. Typically displayed with bullets.
  • Ordered Lists (<ol>): For items where the sequence is important. Typically displayed with numbers or letters.
  • Description Lists (<dl>): For pairs of terms and descriptions (like a dictionary or key-value pairs).

An unordered list starts with the <ul> tag, and each list item starts with the <li> (list item) tag.

<!DOCTYPE html>
<html lang="en">
<head><title>Unordered List</title></head>
<body>
<h2>Shopping List</h2>
<ul>
<li>Milk</li>
<li>Bread</li>
<li>Eggs</li>
</ul>
</body>
</html>

By default, browsers usually render <li> elements within a <ul> with bullet points.

You should use CSS to control the appearance of list markers (bullets). The list-style-type property is commonly used.

CSS Example:

ul.checklist {
list-style-type: square; /* Use squares instead of bullets */
}
ul.no-bullets {
list-style-type: none; /* Remove bullets entirely */
padding-left: 0; /* Remove default browser padding */
}

Common list-style-type values include: disc (default), circle, square, none.

An ordered list starts with the <ol> tag, and each item also uses the <li> tag.

<!DOCTYPE html>
<html lang="en">
<head><title>Ordered List</title></head>
<body>
<h2>Instructions</h2>
<ol>
<li>Open the package.</li>
<li>Read the manual.</li>
<li>Assemble the product.</li>
</ol>
</body>
</html>

Browsers typically render items in an <ol> with sequential numbering (1, 2, 3…).

While HTML has a deprecated type attribute (<ol type="A">), the modern approach is to use the CSS list-style-type property to control the numbering format.

CSS Example:

ol.upper-alpha {
list-style-type: upper-alpha; /* A, B, C... */
}
ol.lower-roman {
list-style-type: lower-roman; /* i, ii, iii... */
}
ol.custom-start {
/* You can also change the starting number with the 'start' attribute in HTML */
/* <ol start="5"> ... </ol> */
}

Common list-style-type values for ordered lists include: decimal (default), decimal-leading-zero, lower-roman, upper-roman, lower-alpha, upper-alpha.

Description lists are used to group terms (<dt> - description term) with their corresponding descriptions (<dd> - description details). The entire list is wrapped in a <dl> tag.

<!DOCTYPE html>
<html lang="en">
<head><title>Description List</title></head>
<body>
<h2>Glossary</h2>
<dl>
<dt>HTML</dt>
<dd>HyperText Markup Language - used for structuring web content.</dd>
<dt>CSS</dt>
<dd>Cascading Style Sheets - used for styling web content.</dd>
<dt>JavaScript</dt>
<dd>A programming language used to add interactivity to web pages.</dd>
</dl>
</body>
</html>

You can place lists inside other list items to create nested structures or outlines.

<ul>
<li>Coffee</li>
<li>Tea
<ul> <!-- Nested List -->
<li>Black tea</li>
<li>Green tea
<ol> <!-- Nested Ordered List -->
<li>Sencha</li>
<li>Matcha</li>
</ol>
</li>
</ul>
</li>
<li>Milk</li>
</ul>

List items (<li>) can contain various HTML elements, including paragraphs, images, links, and other lists.

Lists are often used for navigation menus. CSS can be used to display list items horizontally instead of vertically.

HTML:

<nav>
<ul class="horizontal-menu">
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>

CSS:

.horizontal-menu {
list-style-type: none; /* Remove bullets */
padding: 0;
margin: 0;
display: flex; /* Arrange items in a row */
background-color: #333; /* Example background */
}
.horizontal-menu li {
/* No specific styling needed here for layout with flex */
}
.horizontal-menu li a {
display: block; /* Make the whole area clickable */
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.horizontal-menu li a:hover {
background-color: #111;
}

Flexbox (display: flex) provides a powerful and flexible way to create horizontal layouts.

  • Use <ul> for unordered lists (order doesn’t matter).
  • Use <ol> for ordered lists (sequence matters).
  • Use <dl> for term/description pairs.
  • Use <li> for items within <ul> and <ol>.
  • Use <dt> for terms and <dd> for descriptions within <dl>.
  • Use CSS (list-style-type) for styling list markers.
  • Use CSS (e.g., Flexbox or Grid) to create horizontal list layouts.
  • Lists can be nested within other lists.
  • List items can contain other HTML elements.
TagDescription
    Defines an unordered list.
      Defines an ordered list.
    1. Defines a list item.
      Defines a description list.
      Defines a term/name in a description list.
      Defines a description/value in a description list.