CSS Tables
CSS provides several properties to control the layout and appearance of HTML tables, making them easier to read and more visually appealing.
Table Borders
To specify table borders in CSS, use the border property. The border-collapse: collapse; property is key to making the borders look clean by collapsing them into a single border.
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}Table Width and Height
The width and height properties can be used to set the size of the table, as well as individual cells.
Example
table { width: 100%; }
th { height: 70px; }Text Alignment
Use text-align to control the horizontal alignment (left, right, center) and vertical-align to control the vertical alignment (top, middle, bottom) of content within table cells.
Example
td { text-align: center; }
th { vertical-align: bottom; }Zebra-Striped Tables
Zebra-striping (adding alternating background colors to rows) makes tables much easier to read. This can be done easily with the :nth-child(even) or :nth-child(odd) pseudo-class.
Example
tr:nth-child(even) {
background-color: #f2f2f2;
}Test Yourself with an Exercise
Which property makes table borders collapse into a single border?