Lesson 27 of 51 · Module 4, lesson 6 of 6 · 3 min

Cells that span rows and columns

Make a cell cover several columns or rows, and count cells so the table does not break.

What you'll learn

  • How to make a cell span several columns with colspan
  • How to make a cell span several rows with rowspan
  • How to count cells so that the table does not break

Why this matters

Real timetables and price lists often have a cell that stretches across two columns or down two rows. HTML can do this — but only if you remove the cells that the big cell covers. This lesson shows how to count them.

The two attributes

Two small timetables. In the first, Monday's cell "Project work (2 hours)" has colspan="2" and covers both the 9:00 and 10:00 columns, so Monday's row has only two cells in the code instead of three. In the second, a "Holiday" cell has rowspan="2" and covers the Morning column for Monday and Tuesday, so Tuesday's row leaves out its Morning cell.

  • colspan="2" — the cell is two columns wide. Remove one cell from the same row.
  • rowspan="2" — the cell is two rows high. Remove one cell from the row below.

A timetable

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Timetable</title>
</head>
<body>
  <table>
    <caption>Class timetable</caption>
    <thead>
      <tr>
        <th scope="col">Day</th>
        <th scope="col">9:00</th>
        <th scope="col">10:00</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Monday</th>
        <td colspan="2">Project work</td>
      </tr>
      <tr>
        <th scope="row">Tuesday</th>
        <td>HTML</td>
        <td>CSS</td>
      </tr>
      <tr>
        <th scope="row">Wednesday</th>
        <td rowspan="2">Self-study</td>
        <td>Review</td>
      </tr>
      <tr>
        <th scope="row">Thursday</th>
        <td>Quiz</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

What you'll see: "Project work" starts under 9:00 and runs into the 10:00 column. "Self-study" sits between the Wednesday and Thursday rows. Without borders (a CSS job) the spans are hard to see — open the developer tools and point at a cell in the Elements panel: the browser outlines exactly the area it covers. The figure above shows the same idea with borders.

The "Class timetable" table. On Monday, "Project work" spans both the 9:00 and 10:00 columns. On Wednesday and Thursday, "Self-study" spans two rows in the 9:00 column; the 10:00 column shows "Review" on Wednesday and "Quiz" on Thursday.

Count the cells

Each row must still add up to the number of columns (here: 3).

RowCells in codeAdds up to
MondayDay + colspan="2"1 + 2 = 3
TuesdayDay + HTML + CSS3
WednesdayDay + rowspan="2" + Review3
ThursdayDay + Quiz (9:00 is filled from above)1 + 1 + 1 = 3

Keep it simple

Spanning cells make tables harder to read for everyone, and especially with a screen reader. Use them only when the data needs them. If a table becomes complicated, split it into two simpler tables.

Try it yourself

  1. Select the example and choose Try it.
  2. Add a Friday row where one activity fills both columns.
  3. Break it on purpose: add a third cell to Monday's row. The table grows an extra column. Remove it again.

Common mistakes

  • Not removing the covered cell. The row pushes out an extra column. Fix: count each row.
  • Removing a cell from the wrong row with rowspan. Fix: the cell is removed from the row below, in the same column.
  • Using colspan to make a title across the top. Fix: use <caption> for the title.

Check yourself

  1. A row has 4 columns. One cell has colspan="3". How many other cells does that row need?
  2. With rowspan="2" in Monday's row, which row loses a cell?
  3. What should you do when a table becomes hard to read?

Answers

  1. One (3 + 1 = 4).
  2. The next row (Tuesday's), in the same column.
  3. Split it into simpler tables.

Next: that completes lists and tables. Practise both in the next exercise.

Practice project

a study timetable and to-do list

Goal: plan your own week of study with an accessible table and a list.

Your website after this module

Outcome: your site has a page you will really use: your study plan for this week, as a table and lists.

Your my-website folder: the pages from module 3, and plan.html, linked to your home page.

Use your real week. The plan is about you, whatever the topic of your site.

Warm up first (optional): Nested Packing List and Class Timetable Table in Work Space, if it is in your menu. Work Space checks your code at once, so you know you are ready before you build.

What to do

Create plan.html in your my-website folder, with the full skeleton. It must have:

  1. An <h1>: "My study week".
  2. A table of your study plan:
    • a <caption>;
    • a header row in <thead> with at least three columns, each header a <th scope="col">;
    • at least four days in <tbody>, each day name a <th scope="row">;
    • one cell that uses colspan or rowspan correctly;
    • a total row in <tfoot> (for example total minutes). Check your sum.
  3. An <h2> "This week I will" and an ordered list of at least three goals, in order of priority.
  4. An <h2> "Things I need" and a bulleted list with one item that has its own inner list.
  5. A link from plan.html to your index.html.

Open the page from your folder and check it. Paste your code below and submit.

Checklist before you submit

  • Every row of the table adds up to the same number of columns.
  • Every header cell is a <th> with scope.
  • The total is correct.
  • The inner list is inside an <li>.
  • You did not type numbers into the ordered list items.

Your mentor reviews every submission. The exercise counts as done when it is marked Passed.

Starter code

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My study week</title>
</head>
<body>
  <h1>My study week</h1>
  <table>
    <caption>YOUR CAPTION</caption>
    <thead>
      <tr>
        <th scope="col">Day</th>
        <th scope="col">Topic</th>
        <th scope="col">Minutes</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Monday</th>
        <td>TOPIC</td>
        <td>30</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

Build it in your own folder, on your computer. ZAM students hand this project in on their dashboard, and their mentor reviews it.

Chat with us on WhatsApp