HTML Tables: Structure, Styling, and Modern Best Practices

This article covers HTML tables: how to mark up rows and columns of data, how to add captions and group rows, how to merge cells, and how to style tables with CSS.

If you're new here, the earlier articles "What is HTML?" and "HTML Structure and Semantics" cover the basics of HTML and the elements that give a page its shape. You don't need them to follow along here, but they give helpful context.

What Are Tables For?

A table is for displaying tabular data: data that naturally lines up in rows and columns. Think of spreadsheets, schedules, price lists, sports standings, and comparison charts. Anything where the relationship between a row and a column carries meaning is a good fit for a table.

Here's an example of the kind of data a table is built for:

Without any styling, the result might look pretty plain, with just text lined up in rows. We'll add borders, colors, and spacing with CSS shortly. For now, focus on the structure: a <table> contains rows, each row contains cells, and each cell contains a piece of data.

By the way, in older websites you might see tables used to lay out entire pages. We'll come back to that (and why it fell out of use) near the end of this article.

The Basic Building Blocks

A table is built out of three main tags:

  • <table> - the table itself.
  • <tr> - a table row (think "tr" for "table row").
  • <td> - a table data cell, the actual content of a cell.

These three are enough to build a working table. Our example above also used one more tag, and we'll use it a lot in this article:

  • <th> - a table header cell. Used for column or row headings.

Header cells are optional, and a table works fine without them. But using them makes the table more meaningful, as we'll see in a moment.

The structure is always the same: a <table> contains <tr> rows, and each row contains <th> or <td> cells. You don't write columns directly. They emerge naturally from the cells lining up across each row.

Notice that the first row uses <th> instead of <td>. Browsers display <th> in bold and centered by default, but the more important point is the meaning: <th> tells the browser, screen readers, and search engines "this cell is a heading for the column (or row) it belongs to." This is the same idea we saw with <strong> vs <b> in the previous article: the right tag carries semantic meaning, not just visual formatting.

<caption>: Giving Your Table a Title

Tables often need a title that tells the reader what the table is about. That's what <caption> is for. It goes inside <table>, as the very first child:

By default, the caption appears just above the table, centered. You could also put a regular <h3> heading above the table instead, and visually they'd look similar. But <caption> is tied to the table semantically: screen readers announce it as the table's title, and the relationship between caption and table is clear without you having to spell it out.

<thead>, <tbody>, <tfoot>: Grouping Rows

When tables get longer, it helps to group rows by their role. HTML provides three tags for that:

  • <thead> - the header rows (typically the column titles).
  • <tbody> - the main body rows (the actual data).
  • <tfoot> - the footer rows (totals, summaries, etc.).

Visually, this often looks the same as a plain table. So why bother? A few practical reasons:

  • Screen readers can use the grouping to announce "header" or "footer" appropriately.
  • When a long table is printed, browsers can repeat <thead> at the top of each printed page automatically.
  • It's easier to target each group with CSS (for example, giving the header a different background color).

Note: You don't have to use <thead>, <tbody>, and <tfoot>. For a small, simple table, plain <tr> rows are perfectly fine. But once your table has dozens of rows, or has totals at the bottom, grouping makes the markup easier to read and easier to style.

Merging Cells: colspan and rowspan

Sometimes a single cell needs to span multiple columns or rows. That's what the colspan and rowspan attributes are for.

  • colspan="N" - the cell stretches across N columns.
  • rowspan="N" - the cell stretches across N rows.

colspan

Let's say you want a single header cell that covers two columns:

The "Price" header sits on top of the two sub-columns "Regular" and "Sale". Notice that on the row containing the merged "Price" cell, there are only two cells written, not three, because the second cell takes up the space of two.

rowspan

rowspan works the same way, just vertically:

The "Monday" cell stretches over two rows, so we don't repeat it on the second row's markup. You can combine colspan and rowspan on the same cell if you need to.

Tip: When you start using colspan and rowspan, it's easy to lose count of how many cells should be on each row. If your table looks broken, double-check that the cell counts add up correctly once you account for the spans.

Styling Tables with CSS

This is where things get a little historical. In early HTML, you styled tables by adding attributes directly to the tags: border, bgcolor, background, align, width, and so on. You'll still see those attributes in old tutorials and old codebases.

Almost all of those presentational attributes are now obsolete. They might still work in browsers (browsers are forgiving), but the official recommendation is to handle styling with CSS instead. We'll go through each one and show how to do the same thing with CSS.

Borders

In the old days, you'd add a border like this:

<!-- Old way (avoid in new code) -->
<table border="1">
  ...
</table>

This still draws a thin border, but it's the only thing that border attribute can do. You can't change the color, thickness, or style. Instead, we use the CSS border property:

Two things to note here:

  • We applied border to th and td, so each cell gets its own border.
  • We added border-collapse: collapse; on the table. Without this, each cell's border sits next to the next cell's border, so you'd see a double line between cells. border-collapse: collapse merges adjacent borders into one, which is usually what you want.

Don't worry if these CSS properties feel new. CSS is covered in more detail in "What is CSS?". For now, the takeaway is: borders are a CSS job, not an HTML attribute.

Background Color

The old bgcolor attribute looked like this:

<!-- Old way (avoid in new code) -->
<table bgcolor="#f0f0f0">
  <tr bgcolor="#cccccc">
    <th>Product</th>
    <th>Price</th>
  </tr>
</table>

The CSS equivalent is the background-color property. And usually, instead of coloring every row by hand, we use a CSS selector to alternate row colors automatically:

The tbody tr:nth-child(even) selector picks every even-numbered row in the body and gives it a light gray background. This kind of "zebra striping" is much easier to read at a glance, and you only have to write the rule once, no matter how many rows you add later.

Background Image

There used to be a background attribute for setting a background image:

<!-- Old way (obsolete) -->
<table background="paper.png">
  ...
</table>

This attribute was never well supported and is now obsolete. Use the CSS background-image property instead:

table {
  background-image: url("paper.png");
  background-repeat: repeat;
}

Background images on tables aren't very common in practice, but the CSS equivalent is good to know. And the same property works on any element, not just tables, which is one of the nice things about CSS.

Text Alignment

In old HTML, you'd right-align numbers like this:

<!-- Old way (avoid in new code) -->
<table>
  <tr>
    <td>January</td>
    <td align="right">12,300</td>
  </tr>
</table>

The align attribute on <td>, <th>, <tr>, and <col> is obsolete. Use the CSS text-align property instead:

Right-aligning numeric columns is a common convention. It makes the digits line up by place value, so values are easier to compare. CSS makes this kind of styling a one-line rule, applied consistently to every row.

Width

And finally, the old width attribute:

<!-- Old way (avoid in new code) -->
<table width="600">
  <tr>
    <td width="200">Name</td>
    <td width="400">Description</td>
  </tr>
</table>

Use the CSS width property instead. And rather than setting the width of every cell individually, you can use <colgroup> and <col> to set column widths in one place:

<colgroup> wraps one or more <col> elements, and each <col> represents a column in the table. Setting the width on a <col> applies to every cell in that column, so you don't have to repeat the width on each <td>.

Using percentages (like 30% and 70%) lets the table adjust as the screen resizes, which works well on phones and tablets. Fixed pixel widths can also work, but they're less flexible on small screens.

That said, <colgroup> doesn't come up that often, so it's fine if it looks unfamiliar. Setting the width directly on th or td cells with CSS works just as well, especially for small tables.

Note: The border attribute on <table> hasn't been fully removed from the spec. It's allowed in a limited form, mostly for backwards compatibility. But bgcolor, background, align, and width on table elements are flagged as obsolete. For new projects, using CSS keeps all the styling in one place, which makes it much easier to change the design later.

Why Tables Aren't Used for Page Layout Anymore

Earlier we mentioned that older websites sometimes used tables to lay out entire pages. Here's what that looked like, and why it fell out of use. In the late 1990s and early 2000s, tables were the most reliable way to position things on a web page. CSS layout existed, but browser support was inconsistent, so developers built whole sites out of nested tables:

<!-- Don't do this in new projects -->
<table>
  <tr>
    <td colspan="2">Header</td>
  </tr>
  <tr>
    <td>Sidebar</td>
    <td>Main content</td>
  </tr>
  <tr>
    <td colspan="2">Footer</td>
  </tr>
</table>

This works visually, but it causes a few problems:

  • Screen readers announce tables in a special way ("table with 2 columns and 3 rows…") so that users can move through the data cell by cell. When there's no real tabular data, that announcement just gets in the way, and the page becomes harder to navigate.
  • The layout doesn't adapt well to small screens. On a phone, the sidebar and main content end up squeezed into narrow columns instead of stacking on top of each other.
  • Updating the design later means rearranging table cells, which is much more tedious than tweaking CSS.

Today, the standard tools for page layout are Flexbox and Grid, both part of CSS, and we'll get to those in later articles. The short version: tables are for data, and CSS is for layout.

A Side Note: Tables and Screen Readers

This last section is more of a "good to know" than something you need right away, so feel free to skim it and come back later. Tables can be tricky for screen reader users: imagine listening to a 50-row spreadsheet read out cell by cell. A few small things in your markup can make that experience nicer:

<th> for Header Cells

We've used <th> throughout this article, and this is one place where it pays off. A <td> with bold text can look identical on screen, but <th> is what tells assistive technology "this cell labels the column (or row) it belongs to." When the user reads a data cell, the screen reader can announce the corresponding header along with it.

The scope Attribute

When a header could apply to either a column or a row, the scope attribute makes the relationship explicit:

<table>
  <tr>
    <th scope="col">Product</th>
    <th scope="col">Regular</th>
    <th scope="col">Sale</th>
  </tr>
  <tr>
    <th scope="row">Apple</th>
    <td>$1.20</td>
    <td>$0.90</td>
  </tr>
  <tr>
    <th scope="row">Banana</th>
    <td>$0.50</td>
    <td>$0.40</td>
  </tr>
</table>

scope="col" means "this is a column header," and scope="row" means "this is a row header." For simple tables, browsers can usually figure this out on their own, but for tables with merged cells or unusual layouts, scope removes any ambiguity.

<caption> Helps Here Too

As we covered earlier, a <caption> gives the table a title that screen readers can announce. Even a short caption like "Quarterly revenue, 2026" helps users decide whether to dive into the table or skip past it.

Putting It All Together

Let's pull everything we've covered into one realistic example: a table with a caption, header and body groups, a merged header cell, alternating row colors, and right-aligned numbers:

Every piece of styling here lives in CSS, not as old-style attributes on the table elements. The HTML stays clean and focused on structure, the CSS handles the look, and assistive technology has all the semantic information it needs to read the table aloud.

Summary

  • Tables are for tabular data, not for page layout.
  • The basic structure is <table> > <tr> > <td> (or <th> for headers).
  • <caption> gives the table a title that's tied to it semantically.
  • <thead>, <tbody>, and <tfoot> group rows by their role (useful for long tables, printing, and styling).
  • colspan and rowspan let a single cell span multiple columns or rows.
  • Old presentational attributes (border, bgcolor, background, align, width) are obsolete. Use CSS instead: border + border-collapse, background-color, background-image, text-align, and width.
  • <colgroup> and <col> let you set column-wide styles (like width) in one place.
  • <th>, the scope attribute, and <caption> also help screen readers make sense of a table.

Try It Yourself

Take a small dataset you care about (your weekly schedule, a list of favorite books, a comparison of three phones) and try marking it up as a table. Add a caption, use <th> for the headers, and style the borders and row colors with CSS. Once you have the basics working, experiment with colspan, rowspan, and column widths.

Open the SyncFiddle Editor and build your own table