Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 58 ยท Content

Tables

Style HTML tables with Bootstrap 5.3: the table class, striped rows, hover, borders, small tables, contextual colors, responsive wrappers and group dividers.

Browsers draw HTML tables with almost no styling: no padding, no lines, tight text. Bootstrap's table classes turn plain <table> markup into something readable with one class, and then let you opt into stripes, hover highlights, borders, colors and scrolling on small screens.

This lesson assumes you know the basic table elements (table, thead, tbody, tr, th, td). In Bootstrap, tables are opt-in: a bare <table> gets only a few reboot rules. You must add the class table to get the styling.

The basic table

Add class="table" and use proper <thead> and <tbody> sections. Use <th scope="col"> for column headings and <th scope="row"> for row headings so assistive technology can connect each cell with its headings.

A basic Bootstrap table

The <caption> titles the table for everyone, including screen reader users. By default it sits below the table; add caption-top to the table to move it above.

flowchart TD
  T["table (required base class)"] --> S["Row style:<br/>table-striped, table-hover"]
  T --> B["Borders:<br/>table-bordered, table-borderless"]
  T --> Z["Density:<br/>table-sm"]
  T --> C["Color:<br/>table-dark, table-primary, ..."]
  T --> R["Wrapper:<br/>table-responsive"]

Every other table class modifies the base table class.

Striped rows, columns and hover

Alternating row shading helps the eye follow a row across a wide table. Add table-striped. For shaded columns use table-striped-columns. Add table-hover to highlight the row under the pointer.

Striped and hover

Move the pointer over the first table to see the hover effect. Stripes and hover work together; the hover color wins on the row you are pointing at.

Borders, density and dark tables

  • table-bordered draws borders on all sides of every cell.
  • table-borderless removes the lines.
  • table-sm halves the cell padding for dense data.
  • table-dark gives a dark table (in 5.3 you can instead set data-bs-theme="dark" on the table; see Color Modes and Dark Mode).

You can also tint the border with the border color utilities, for example border-primary, which works on table-bordered tables.

Bordered, small and dark

Contextual colors

The theme colors work on tables too. Put table-primary, table-success, table-danger, table-warning, table-info, table-light or table-dark on a whole table, a <tr> or a single <td>/<th>.

Colored rows and cells

Contextual classes override stripes and hover for that row or cell, because each one sets the table's background variables (such as --bs-table-bg and the accent overlay --bs-table-accent-bg used for stripes and hover). table-active is the neutral highlight for a selected row. As with other colors, do not rely on color alone: the status words in the cells above carry the meaning.

Group dividers

table-group-divider draws a thicker line above the element it is on. Put it on a <tbody> to separate the header from the body, or to separate groups of rows:

table-group-divider

A table may have several <tbody> elements, which is a good way to group rows semantically and then separate them visually.

Responsive tables

A wide table does not shrink the way text does; it pushes the page wider than the screen. Wrap it in a div with table-responsive and the wrapper scrolls horizontally instead. Use table-responsive-md (and the other breakpoints) to scroll only below that breakpoint, so the table sits normally on larger screens.

table-responsive wrapper

The text-nowrap class stops cells from wrapping so the table stays wide enough to demonstrate scrolling. Two cautions: the wrapper must be a div around the table, not a class on the table; and the dropdown menus inside a scrolling wrapper can be clipped, so avoid putting them in responsive tables where possible.

Common mistakes

  • Forgetting the table class. table-striped alone does nothing without it.
  • Using tables for page layout. Use the grid (The Grid System). Tables are for tabular data.
  • Leaving out <thead>, <th> and scope. They are what make a table accessible.
  • Putting table-responsive on the <table>. It belongs on a wrapping element.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which class is required on every Bootstrap-styled table?

    The base table class provides the styling; striped, hover, bordered and the other classes only modify it.
  2. 2. How do you make a wide table scroll horizontally on small screens?

    table-responsive on a wrapper element adds overflow-x scrolling. The class goes on the wrapper, not on the table.
  3. 3. What does table-group-divider do?

    It draws a stronger separator line above a tbody, thead or tfoot, which visually separates groups of rows.
  4. 4. Which class highlights the row under the pointer?

    table-hover highlights rows on hover. table-active marks a row as permanently highlighted.

Summary

  • Add table to a <table> to style it; then add table-striped, table-hover, table-bordered, table-borderless and table-sm as needed.
  • table-striped-columns shades columns; caption-top moves the caption above.
  • Contextual classes (table-success, table-danger, and so on) color a table, row or cell; table-active marks a selected row.
  • table-group-divider separates groups of rows.
  • Wrap wide tables in table-responsive (or table-responsive-md) so they scroll.
  • Use thead, th and scope for accessibility and tables for data, not layout.

Next lesson: Spacing Utilities.