CSS for Tables

The table element, more than any other HTML element, requires additional CSS styling to reach its full potential.

Tables Concepts

As discussed in HTML for Tables, a table is meant to represent tabular data. A table without any CSS styles applied will look something like this:

Table caption
Head cell one infoHead cell two infoHead cell three info
Cell one infoCell two infoCell three info
Cell four infoCell five infoCell six info
Cell seven infoCell eight infoCell nine info
Foot cell one infoFoot cell two infoFoot cell three info

It puts the information into rows and columns, but it’s difficult to read and identify the different parts and structure of the table. With a little styling we can make it much easier for the visitor to recognize the important information in the table, and how it all relates to each other.

Table caption
Head cell one infoHead cell two infoHead cell three info
Cell one infoCell two infoCell three info
Cell four infoCell five infoCell six info
Cell seven infoCell eight infoCell nine info
Foot cell one infoFoot cell two infoFoot cell three info

The styles applied are fairly simple, but this underscores the philosophy of this site. Use HTML first, and add in CSS (and JavaScript) where it will make your site better.

.table {
  border: 1px solid black;
  font-family: Arial, Helvetica, sans-serif;
  text-align: center;
  table-layout: fixed;
}
.table caption {
  background-color: black;
  color: white;
  padding: .25rem;
}
.table th {
  background-color: gainsboro;
  padding: .25rem;
}
.table td {
  background-color: whitesmoke;
  padding: .25rem;
}

For more information about why the second table looks so much better see Getting Better at Design.

CSS used in tables

The following standard CSS properties (border, padding, text-align, and background-color) are the most common properties that you’ll end up using on tables.

border

Adding a border to different elemments of the table can help differentiate the elements from each other.

padding

The default styles on a table doesn’t include cell padding. Adding just a little helps to differentiate the information in each cell.

text-align

The convention used in most tables is that digits are right-aligned, and other forms of data are left-aligned.

Alignment examples
leftcenter$3.00
leftcenter$6.00
leftcenter$9.00

background-color

The background-color property when used in tables can help differentiate captions and headers from normal table cells.

Table caption
Head cell one infoHead cell two infoHead cell three info
Cell one infoCell two infoCell three info
Cell four infoCell five infoCell six info
Cell seven infoCell eight infoCell nine info
Foot cell one infoFoot cell two infoFoot cell three info

Table display properties

  • display: inline-table
  • display: table
  • display: table-caption
  • display: table-row
  • display: table-cell
  • display: table-column
  • display: table-column-group
  • display: table-header-group
  • display: table-footer-group

Using the different table display properties it’s entirely possible to recreate a table structure using non-table elements.

Fake Table Caption
Fake Header Cell
Fake Header Cell
Fake Header Cell
Fake Table Cell
Fake Table Cell
Fake Table Cell

Even though it’s possible, you’re still better off using real table elements when you need a table. The best actual use case for these properties is if you set the display property to none on a table or table element, and need to set it back to the original value.

Table Specific Properties

The table specific properties can only be used on tables or table elements.

border-collapse

The border-collapse property controls the space between cell borders. By setting it to collapse you can remove that spacing.

border-collapse: collapse; Example
cellcellcell
cellcellcell
cellcellcell

border-spacing

The border-spacing property controls the amount space between cell borders. Using border-spacing: 0px; is effectively the same as using border-collapse: collapse;.

border-spacing: 10px; Example
cellcellcell
cellcellcell
cellcellcell

caption-side

Using caption-side can change the location of the table’s caption. The default value is top.

caption-side: bottom; Example
cellcellcell
cellcellcell
cellcellcell

empty-cells

The empty-cells property controls how the table handles the display of empty cells. The default value is show.

empty-cells: hide; Example
cellcell
cellcell
cellcell

table-layout

The table-layout property can even out the columns on a table, when the value is set to fixed. The default style is auto.

table-layout: auto; Example
Other cellcellLast cell
4111cell
867-5309cell42
table-layout: fixed; Example
Other cellcellLast cell
4111cell
867-5309cell42

Table Helpers

Zebra Stripes

Using the :nth-child(odd) pseudo-class you can have the table row colors alternate.

.table.zebra tr:nth-child(odd) td {
  background-color: gainsboro;
}
Zebra Stripes Example
cellcellcell
cellcellcell
cellcellcell
cellcellcell
cellcellcell
cellcellcell

Highlighting

Using the :hover pseudo-selector you can set a row to change color on hover, highlighting the row that the visitor is focused on.

.table.hightlighting tr:hover td {
  background-color: yellow;
}
Highlighting Example
cellcellcell
cellcellcell
cellcellcell
cellcellcell
cellcellcell
cellcellcell

Responsive

Wide tables on small devices can be a challenge. For exceptionally wide tables, you might consider wrapping the table in an element with width: 100%; overflow-x: auto; values.

width: 100%;
overflow-x: auto;
Responsive Example
responsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cell
responsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cell
responsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cell
responsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cellresponsive cell

Types of Tables

Tables are usually intimately tied to the data within them. Various types of tables include:

Multiplication

Multiplication Table
× 0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 1 2 3 4 5 6 7
2 0 2 4 6 8 10 12 14
3 0 3 6 9 12 15 18 21

Calendar

January 2022
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Rows

Internet Movies
Name Actor Year
War Games Matthew Broderick 1983
The Net Sandra Bullock 1995
Hackers Angelina Jolie 1995
The Matrix Keanu Reeves 1999

Lookup

Alignment
Lawful Neutral Chaotic
Good Lawful Good Neutral Good Chaotic Good
Neutral Lawful Neutral True Neutral Chaotic Neutral
Evil Lawful Evil Neutral Evil Chaotic Evil

Comparison

Space Opera
Star Trek Star Wars Dune The Foundation Firefly Cowboy Bebop
Galactic Empire
Rebels
Bountyhunters
Robots

CRUD Dashboard

An Author Database
ID Name Description Edit Delete
12 Edgar Allan Poe Writer, poet, editor, and literary critic.
13 William Hope Hodgson Author of horror, fantastic fiction, and science fiction.
15 H.P. Lovecraft Writer of weird, science, fantasy, and horror fiction.
28 Isaac Asimov Writer and professor of biochemistry at Boston University.
29 Ray Bradbury Author and screenwriter of the fantastic.
32 Frank Herbert Author, journalist, photographer,and ecological consultant.
42 Douglas Adams English author, screenwriter, essayist, humorist, satirist and dramatist.

Invoice

Web Development Invoice
Item Units Cost
HTML 1 $16.00
Advice $0.02
Red Ballons 99 $0.99
Whitespace 12 $0.00
Total:$17.01