CSS Table
Using CSS can make HTML tables more attractive.
| Company | Contact | Country |
|---|---|---|
| Alfreds Futterkiste | Maria Anders | Germany |
| Berglunds snabbköp | Christina Berglund | Sweden |
| Centro comercial Moctezuma | Francisco Chang | Mexico |
| Ernst Handel | Roland Mendel | Austria |
| Island Trading | Helen Bennett | UK |
| Königlich Essen | Philip Cramer | Germany |
| Laughing Bacchus Winecellars | Yoshi Tannamuri | Canada |
| Magazzini Alimentari Riuniti | Giovanni Rovelli | Italy |
| North/South | Simon Crowther | UK |
| Paris spécialités | Marie Bertrand | France |
| The Big Cheese | Liz Nixon | USA |
| Vaffeljernet | Palle Ibsen | Denmark |
Table Borders
To specify CSS table borders, use the border property.
The following example specifies a black border for the th and td elements of a table:
Example
table
,
th
,
td
{
border:
1
px
solid
black
;
}
Try it »
Note that the table in the example above has double borders. This is because the table and the th/td elements have independent borders.
To display a single border for a table, use the border-collapse property.
Collapsed Borders
The border-collapse property sets whether the table borders are collapsed into a single border or separated:
Example
table
{
border-collapse:
collapse
;
}
table
,
th
,
td
{
border:
1
px
solid
black
;
}
Try it »
Table Width and Height
The width and height properties define the width and height of a table.
The following example sets a table with a width of 100% and a height of 50 pixels for the th elements:
Example
table
{
width:
100
%
;
}
th
{
height:
50
px
;
}
Try it »
Table Text Alignment
Text alignment and vertical alignment properties in tables.
The text-align property sets the horizontal alignment, to the left, right, or center:
Example
td
{
text-align:
right
;
}
Try it »
The vertical-align property sets the vertical alignment, such as top, bottom, or middle:
Example
td
{
height:
50
px
;
vertical-align:
bottom
;
}
Try it »
Table Padding
To control the spacing between the border and the table content, use the padding property of the td and th elements:
Example
td
{
padding:
15
px
;
}
Try it »
Table Colors
The following example specifies the border color, and the text and background colors of the th elements:
Example
table
,
td
,
th
{
border:
1
px
solid
green
;
}
th
{
background-color:
green
;
color:
white
;
}
Try it »
More examples
Create a Personalized Table
This example demonstrates how to create a personalized table.
Set the position of the table caption
This example demonstrates how to position the table caption.