Apollo Editor
Windows 10 & 11 Buy now · $69.95
HTML & CSS basics

How to Make a Table in HTML: Rows, Headers, Merged Cells and Styling

How to make a table in HTML: rows, header cells, merged cells with colspan and rowspan, borders and spacing with CSS, and tables that work on phones.

Updated 5 minute read The Apollo Editor team

To make a table in HTML, wrap it in a <table> element, add one <tr> (table row) per row, and put each cell inside the row as a <td> for data or a <th> for a header. Group the header row in <thead> and the data rows in <tbody>. Then use CSS for borders, spacing and colors.

This guide builds a table step by step, then covers merged cells with colspan and rowspan, styling, accessibility, and keeping wide tables usable on phones.

A basic HTML table, step by step

Here is a small table of opening hours with a header row and two data rows. Each line below is one line of the HTML:

  • <table>
  • <caption>Opening hours</caption>
  • <thead>
  • <tr><th scope="col">Day</th><th scope="col">Open</th><th scope="col">Close</th></tr>
  • </thead>
  • <tbody>
  • <tr><td>Monday</td><td>9:00</td><td>17:00</td></tr>
  • <tr><td>Saturday</td><td>10:00</td><td>14:00</td></tr>
  • </tbody>
  • </table>

The rules are simple:

  1. Every row is a <tr>.
  2. Every cell sits inside a row, never directly inside the table.
  3. Each row should have the same number of cells, unless a merged cell takes up the space (see below).
  4. Header cells use <th>, which browsers show in bold and centered by default.

The optional <caption> gives the table a visible title. It must be the first thing inside the table.

The <thead> and <tbody> groups are not strictly required, but they are worth using. They make the code easier to read, give you simple hooks for CSS, and help some browsers repeat the header row when a long table is printed across several pages.

The table elements at a glance

ElementPurpose
<table>Wraps the whole table
<caption>A title or short description of the table
<thead>Groups the header row or rows
<tbody>Groups the main data rows
<tfoot>Groups summary rows such as totals
<tr>One row
<th>A header cell for a column or row
<td>A normal data cell

Merging cells with colspan and rowspan

Merged cells let one cell stretch across several columns or rows. You do this with two attributes on a <td> or <th>.

Spanning columns with colspan

To make a cell two columns wide, add colspan="2" and remove one cell from that row, because the merged cell now takes its place:

<tr><td>Sunday</td><td colspan="2">Closed</td></tr>

The row still adds up to three columns: one normal cell plus one cell that spans two.

Spanning rows with rowspan

To make a cell two rows tall, add rowspan="2" to it. Then leave out the matching cell in the row below, because the tall cell already fills that spot:

  • <tr><th rowspan="2" scope="rowgroup">Weekend</th><td>Saturday</td><td>10:00</td></tr>
  • <tr><td>Sunday</td><td>Closed</td></tr>

The most common mistake with merged cells is forgetting to remove the cell that is being replaced. That pushes the extra cell out past the edge of the table. If your table has a strange bulge on one side, count the cells in each row, including the space taken by spans.

Styling a table with CSS

Older tutorials use attributes such as border="1" and cellpadding. These are obsolete in HTML5. Use CSS instead, ideally in your external stylesheet. A clean starting point:

  • table { border-collapse: collapse; width: 100%; }
  • th, td { border: 1px solid #ccc; padding: 8px 12px; text-align: left; }
  • thead th { background-color: #f2f4f7; }
  • tbody tr:nth-child(even) { background-color: #fafafa; }

What each part does:

  • border-collapse: collapse joins neighboring cell borders into single lines instead of double ones.
  • padding on cells adds breathing room around the content.
  • nth-child(even) shades every other row (zebra stripes), which makes long tables easier to scan.
  • vertical-align (for example vertical-align: top; on td) controls where content sits in tall cells.

To align numbers neatly, give number columns text-align: right; using a class on those cells.

Making tables accessible

Screen readers rely on table markup to read a table in a sensible way. A few small steps help a lot:

  • Use <th> for headers, not <td> with bold text.
  • Add scope="col" to column headers and scope="row" to row headers.
  • Add a <caption> that says what the table shows.
  • Keep tables for data only. Do not use tables to lay out a page. That was common in the early web, but CSS Flexbox and Grid now do the job far better. See CSS Flexbox vs Grid.

For broader tips, see our guide to website accessibility basics.

Tables that work on phones

A table with many columns will not fit on a narrow screen. The simplest fix is to let the table scroll sideways inside its own box, so the rest of the page stays put. Wrap the table in a container:

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

Then give that container overflow-x: auto; in your CSS:

.table-scroll { overflow-x: auto; }

Other ways to help small screens:

  • Remove columns that are not essential.
  • Shorten header labels.
  • Allow long text to wrap rather than setting fixed widths on cells.
  • Test the page at phone width before you publish.

Our guide on making a website responsive covers testing at different sizes.

The easier way: build tables in Apollo Editor

Hand-counting cells for colspan and rowspan gets tiresome. Apollo Editor, a Windows desktop HTML editor, gives you point-and-click table tools while writing modern HTML and CSS. Use the Table menu or the Insert Table button on the main toolbar.

  • Insert or draw a table, then add or remove rows and columns.
  • Merge and split cells, and resize columns, without counting spans by hand.
  • Set cell padding, borders, background colors, vertical alignment, table width and table styles.
  • Import tabular data, and convert simple tables to and from text.

Split mode shows the table markup beside the page as you edit, so you can see exactly what each change does. Learn more on the features section.

Frequently asked questions

How do I merge cells in an HTML table?

Use colspan to make a cell span several columns and rowspan to make it span several rows, then remove the cells it replaces.

How do I add borders to an HTML table?

Use CSS, for example border: 1px solid on th and td, plus border-collapse: collapse on the table so the lines join neatly.

How do I make a wide table fit on a phone?

Place it inside a container with overflow-x: auto so it scrolls sideways within the page instead of breaking the layout.

Get three guides like this every month, plus software update notices.Subscribe →