← Back to HTML & CSS, page 3

html-css-0054 · Easy fresher

When do you use ul, ol and dl, and which attributes does ol have?

Use ul when order does not matter, ol when it does, and dl for term and description pairs. Only li may be a direct child of ul and ol, and nested lists go inside an li. ol supports start, reversed and type, and CSS controls markers.

Full answer

HTML has three kinds of list.

  • ul is an unordered list. Use it when the order does not matter, such as a list of features or a navigation menu.
  • ol is an ordered list. Use it when the order matters, such as steps in a recipe or a ranking.
  • dl is a description list. It pairs terms (dt) with their descriptions (dd), like a glossary or a set of label and value pairs.

Only li elements may be direct children of ul and ol. To nest a list, place the inner ul or ol inside an li, not beside it.

The ol element has useful attributes: start sets the first number, reversed counts down, and type chooses the marker (1, a, A, i, I). An individual li can take a value to jump to a given number. The look of the marker is a CSS job: list-style-type, list-style-position and list-style: none.

The first list counts 3, 2, 1 because of reversed start="3". The nested ul sits inside the li for Fruit. The navigation uses list-style: none so the bullets disappear, but it is still a list, and screen readers announce "list, 2 items" to help users know what they are entering.

A menu is a list of links, so ul is the right element for it even though it does not look like bullets. Removing the markers with CSS does not change the meaning.

html
<style>
  nav ul { list-style: none; margin: 0; padding: 0; display: flex; gap: 1rem; }
</style>
<ol reversed start="3" id="top3">
  <li>Gold</li>
  <li>Silver</li>
  <li>Bronze</li>
</ol>
<ul>
  <li>Fruit
    <ul><li id="inner">Mango</li></ul>
  </li>
</ul>
<dl>
  <dt>Padding</dt><dd>Space inside the border.</dd>
  <dt>Margin</dt><dd>Space outside the border.</dd>
</dl>
<nav aria-label="Main"><ul><li><a href="/">Home</a></li><li><a href="/fees">Fees</a></li></ul></nav>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • ul is unordered, ol is ordered, dl pairs dt terms with dd descriptions.
  • Direct children of ul and ol must be li elements.
  • Nested lists belong inside an li.
  • ol has start, reversed and type attributes; li can take value.
  • Removing markers with CSS keeps the list semantics.

Common mistakes

  • Putting an inner ul directly inside another ul, outside any li. This is invalid HTML.
  • Using br tags or separate paragraphs to fake a list. Screen readers then do not announce it as a list.
  • Using ol for a list where order does not matter, which suggests a ranking that does not exist.

Follow-ups

  • How do you nest a list inside another list correctly?
  • What is a description list and when would you choose it?

Tips

  • When asked for a menu, say that a ul of links is the usual structure and CSS removes the bullets.
  • Know ol start, reversed and type by heart. They are small details that show hands-on use.