CSS can significantly enhance the appearance of an HTML table.
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 |
To define table borders using CSS, utilize the border property.
The following example demonstrates how to specify a solid border for <table>, <th>, and <td> elements:
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
Example
table, th, td { border: 1px solid; } |
In certain cases, the table above may appear small. To create a table that spans the entire screen width (full-width), apply width: 100% to the <table> element.
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
Example
table { width: 100%; } |
Double Borders: Note that the tables in the examples above have double borders. This occurs because both the table and the <th> and <td> elements have separate borders. To eliminate double borders, refer to the example below. |
The border-collapse property determines whether the borders of table cells should collapse into a single border.
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
Example
table { border-collapse: collapse; } |
If you desire a border solely around the table, apply the border property exclusively to the <table> element.
Firstname | Lastname |
---|---|
Peter | Griffin |
Lois | Griffin |
Example
table { border: 1px solid; } |