CSS Tables

❮ PreviousNext ❯


The look of an HTML table can be greatly improved with CSS:

CompanyContactCountry
Alfreds FutterkisteMaria AndersGermany
Berglunds snabbköpChristina BerglundSweden
Centro comercial MoctezumaFrancisco ChangMexico
Ernst HandelRoland MendelAustria
Island TradingHelen BennettUK
Königlich EssenPhilip CramerGermany
Laughing Bacchus WinecellarsYoshi TannamuriCanada
Magazzini Alimentari RiunitiGiovanni RovelliItaly

Try it Yourself »


Table Borders

To specify table borders in CSS, use the border property.

The example below specifies a solid border for <table>, <th>, and <td> elements:

FirstnameLastname
PeterGriffin
LoisGriffin

Example

table, th, td {
  border: 1px solid;
}

Try it Yourself »


ADVERTISEMENT


Full-Width Table

The table above might seem small in some cases. If you need a table that should span the entire screen (full-width), add width: 100% to the <table> element:

FirstnameLastname
PeterGriffin
LoisGriffin

Example

table {
  width: 100%;
}

Try it Yourself »

Double Borders

Notice that the table in the examples above have double borders. This is because both the table and the <th> and <td> elements have separate borders.

To remove double borders, take a look at the example below.


Collapse Table Borders

The border-collapse property sets whether the table borders should be collapsed into a single border:

FirstnameLastname
PeterGriffin
LoisGriffin

Example

table {
  border-collapse: collapse;
}

Try it Yourself »

If you only want a border around the table, only specify the border property for <table>:

FirstnameLastname
PeterGriffin
LoisGriffin

Example

table {
  border: 1px solid;
}

Try it Yourself »

❮ PreviousNext ❯

CSS Table Size

❮ PreviousNext ❯


Table Width and Height

The width and height of a table are defined by the width and height properties.

The example below sets the width of the table to 100%, and the height of the <th> elements to 70px:

FirstnameLastnameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

table {
  width: 100%;
}

th {
  height: 70px;
}

Try it Yourself »

To create a table that should only span half the page, use width: 50%:

FirstnameLastnameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

table {
  width: 50%;
}

Try it Yourself »

❮ PreviousNext ❯

CSS Table Alignment

❮ PreviousNext ❯


Horizontal Alignment

The text-align property sets the horizontal alignment (like left, right, or center) of the content in <th> or <td>.

By default, the content of <th> elements are center-aligned and the content of <td> elements are left-aligned.

To center-align the content of  <td> elements as well, use text-align: center:

FirstnameLastnameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

td {
  text-align: center;
}

Try it Yourself »

To left-align the content, force the alignment of <th> elements to be left-aligned, with the text-align: left property:

FirstnameLastnameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

th {
  text-align: left;
}

Try it Yourself »


Vertical Alignment

The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or <td>.

By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).

The following example sets the vertical text alignment to bottom for <td> elements:

FirstnameLastnameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

td {
  height: 50px;
  vertical-align: bottom;
}

Try it Yourself »

❮ PreviousNext ❯

CSS Table Style

❮ PreviousNext ❯


Table Padding

To control the space between the border and the content in a table, use the padding property on <td> and <th> elements:

First NameLast NameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

th, td {
  padding: 15px;
  text-align: left;
}

Try it Yourself »


Horizontal Dividers

First NameLast NameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Add the border-bottom property to <th> and <td> for horizontal dividers:

Example

th, td {
  border-bottom: 1px solid #ddd;
}

Try it Yourself »


Hoverable Table

Use the :hover selector on <tr> to highlight table rows on mouse over:

First NameLast NameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

tr:hover {background-color: coral;}

Try it Yourself »


ADVERTISEMENT


Striped Tables

First NameLast NameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:

Example

tr:nth-child(even) {background-color: #f2f2f2;}

Try it Yourself »


Table Color

The example below specifies the background color and text color of <th> elements:

First NameLast NameSavings
PeterGriffin$100
LoisGriffin$150
JoeSwanson$300

Example

th {
  background-color: #04AA6D;
  color: white;
}

Try it Yourself »

❮ PreviousNext ❯

CSS Responsive Table

❮ PreviousNext ❯


Responsive Table

A responsive table will display a horizontal scroll bar if the screen is too small to display the full content:

First NameLast NamePointsPointsPointsPointsPointsPointsPointsPointsPointsPointsPointsPoints
JillSmith505050505050505050505050
EveJackson949494949494949494949494
AdamJohnson676767676767676767676767

Add a container element (like <div>) with overflow-x:auto around the <table> element to make it responsive:

Example

<div style=”overflow-x:auto;”>

<table>
… table content …
</table>

</div>

Try it Yourself »

Note: In OS X Lion (on Mac), scrollbars are hidden by default and only shown when being used (even though “overflow:scroll” is set).


More Examples

Make a fancy table
This example demonstrates how to create a fancy table.

Set the position of the table caption
This example demonstrates how to position the table caption.


Test Yourself With Exercises

Exercise:

Set the border to “2px solid green” for table, th and td elements.

<style>
 {
  : 2px solid green;
}
</style>

<body>
<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
  </tr>
  <tr>
    <td>Peter</td>
    <td>Griffin</td>
  </tr>
  <tr>
    <td>Lois</td>
    <td>Griffin</td>
  </tr>
</table>
</body>

Submit Answer »

Start the Exercise


CSS Table Properties

PropertyDescription
borderSets all the border properties in one declaration
border-collapseSpecifies whether or not table borders should be collapsed
border-spacingSpecifies the distance between the borders of adjacent cells
caption-sideSpecifies the placement of a table caption
empty-cellsSpecifies whether or not to display borders and background on empty cells in a table
table-layoutSets the layout algorithm to be used for a table

❮ PreviousNext ❯