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:
Head cell one info | Head cell two info | Head cell three info |
---|---|---|
Cell one info | Cell two info | Cell three info |
Cell four info | Cell five info | Cell six info |
Cell seven info | Cell eight info | Cell nine info |
Foot cell one info | Foot cell two info | Foot 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.
Head cell one info | Head cell two info | Head cell three info |
---|---|---|
Cell one info | Cell two info | Cell three info |
Cell four info | Cell five info | Cell six info |
Cell seven info | Cell eight info | Cell nine info |
Foot cell one info | Foot cell two info | Foot 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.
left | center | $3.00 |
left | center | $6.00 |
left | center | $9.00 |
background-color
The background-color
property when used in tables can help differentiate captions and headers from normal table cells.
Head cell one info | Head cell two info | Head cell three info |
---|---|---|
Cell one info | Cell two info | Cell three info |
Cell four info | Cell five info | Cell six info |
Cell seven info | Cell eight info | Cell nine info |
Foot cell one info | Foot cell two info | Foot 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.
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.
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
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;
.
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
caption-side
Using caption-side
can change the location of the table’s caption. The default value is top
.
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
empty-cells
The empty-cells
property controls how the table handles the display of empty cells. The default value is show
.
cell | cell | |
cell | cell | |
cell | cell |
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
.
Other cell | cell | Last cell |
4 | 111 | cell |
867-5309 | cell | 42 |
Other cell | cell | Last cell |
4 | 111 | cell |
867-5309 | cell | 42 |
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;
}
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
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;
}
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
cell | cell | cell |
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 cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell |
responsive cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell |
responsive cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell |
responsive cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell | responsive cell |
Types of Tables
Tables are usually intimately tied to the data within them. Various types of tables include:
Multiplication
× | 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
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
Name | Actor | Year |
---|---|---|
War Games | Matthew Broderick | 1983 |
The Net | Sandra Bullock | 1995 |
Hackers | Angelina Jolie | 1995 |
The Matrix | Keanu Reeves | 1999 |
Lookup
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
Star Trek | Star Wars | Dune | The Foundation | Firefly | Cowboy Bebop | |
---|---|---|---|---|---|---|
Galactic Empire | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ |
Rebels | ✗ | ✓ | ✓ | ✗ | ✓ | ✗ |
Bountyhunters | ✗ | ✓ | ✗ | ✗ | ✓ | ✓ |
Robots | ✓ | ✓ | ✗ | ✓ | ✗ | ✗ |
CRUD Dashboard
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
Item | Units | Cost |
---|---|---|
HTML | 1 | $16.00 |
Advice | $0.02 | |
Red Ballons | 99 | $0.99 |
Whitespace | 12 | $0.00 |
Total: | $17.01 |