Skip to the content

Lesson 9 of 296

Lists and tables

What you will learn

  • The three kinds of lists
  • How to build a correct table
  • How to make tables readable for screen readers
  • When not to use a table
On this page

Unordered lists

Use <ul> when the order does not matter. Each item is an <li> (list item).

<ul>
  <li>Milk</li>
  <li>Bread</li>
  <li>Eggs</li>
</ul>

The browser shows a bullet point before each item.

Ordered lists

Use <ol> when the order matters, such as steps.

<ol>
  <li>Boil the water</li>
  <li>Add the pasta</li>
  <li>Cook for 10 minutes</li>
</ol>

The browser numbers the items. Useful attributes: start="5" starts at 5, and reversed counts down.

Description lists

Use <dl> for pairs of terms and descriptions, like a glossary or product details.

<dl>
  <dt>Weight</dt>
  <dd>1.2 kg</dd>
  <dt>Colour</dt>
  <dd>Black</dd>
</dl>

<dt> is the term. <dd> is the description.

Lists inside lists

A list can go inside an <li>.

<ul>
  <li>Fruit
    <ul>
      <li>Apples</li>
      <li>Pears</li>
    </ul>
  </li>
  <li>Vegetables</li>
</ul>

Rule: the only direct child of <ul> or <ol> is <li>. Do not put a <p> or <div> straight inside <ul>.

Most site menus are lists of links. This is the standard way to write them:

<nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/services">Services</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>

A screen reader then says "list, 3 items", which tells the user how big the menu is.

Tables

Use a table for data with rows and columns, like a spreadsheet: prices, timetables, results.

<table>
  <caption>Monthly plans</caption>
  <thead>
    <tr>
      <th scope="col">Plan</th>
      <th scope="col">Price</th>
      <th scope="col">Users</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Basic</th>
      <td>£5</td>
      <td>1</td>
    </tr>
    <tr>
      <th scope="row">Team</th>
      <td>£20</td>
      <td>10</td>
    </tr>
  </tbody>
</table>
ElementMeaning
<table>The whole table
<caption>The title of the table
<thead>The group of header rows
<tbody>The group of data rows
<tfoot>The group of footer rows, such as totals
<tr>One row (table row)
<th>A header cell
<td>A normal data cell (table data)

Making tables accessible

  • Use <th> for headers, not a <td> made bold with CSS.
  • Add scope="col" to column headers and scope="row" to row headers. Screen readers then read "Team, Price, £20", so the user knows what each number means.
  • Add a <caption> so the user knows what the table is about before reading it.

Joining cells

  • colspan="2" makes a cell cover two columns.
  • rowspan="2" makes a cell cover two rows.
<tr>
  <td colspan="2">Total</td>
  <td>£25</td>
</tr>

When not to use a table

Never use a table to lay out a page (for example, a sidebar next to the content). That was common many years ago. It confuses screen readers and is hard to make work on phones. Use CSS Flexbox and Grid for layout. You will learn them in Part 3.

Tables on small screens

Wide tables do not fit on phones. A simple fix is to put the table inside a container that can scroll sideways:

<div class="table-wrap">
  <table>...</table>
</div>

and give .table-wrap the CSS overflow-x: auto;.

Common mistakes

  • Using <ul> for steps where the order matters. Use <ol>.
  • Putting something other than <li> straight inside a list.
  • Using tables for layout.
  • Header cells made with <td> instead of <th>.

How to say it in an interview

"I use ul when order does not matter, ol when it does, and dl for term and description pairs. Navigation menus are usually a list of links inside a nav element. Tables are only for tabular data, never for layout. I use thead, tbody, th with scope and a caption so screen readers can connect each value to its header."

Key words

WordMeaning
<ul>Unordered list (bullets)
<ol>Ordered list (numbers)
<li>One list item
<dl>Description list of terms and descriptions
<th>Table header cell
<td>Table data cell
scopeTells screen readers if a header is for a column or a row
colspan / rowspanMakes a cell cover more than one column or row

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.