Overview
HTML tables can be styled to a point using tag attributes like border, cell-padding, cell-spacing, and width/height.
<table border="1" cellpadding="5" cellspacing="5" width="300">
<tr>
<th></th>
<th></th>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
However this is somewhat limited, and CSS can be used to make your tables look exactly as you intend.
Plus, the styles will apply to all tables site wide, removing the need to style each table individually from within the <table> tag.
Note
The following are basic examples. Towards the bottom of this page you’ll find a complete list of all applicable properties, where you can find more information on the properties discussed, and sometimes find more involved properties not discussed on this page.
Using CSS
Set the width:
table {
width: 100%;
}
Styling and collapsing the border:
By default a table will have a border around it and each cell will have a border around it, and there is space between these borders creating a double-border effect. By using the border-collapse property, these borders are merged into one making them more pleasing to the eye. And to add to the look, we can set the border color, style, and size.
table, th, td {
border: red solid thin;
border-collapse: collapse;
}
Adjusting the text and padding:
th, td {
color: red;
font-size: 12px;
font-weight: bold;
padding: 10px;
text-align: left;
}
Table-Specific Properties
- CSS – border PropertyThe border property is a shorthand property for all the other properties a CSS border can be given.
- CSS – border-collapse PropertyThe border-collapse property sets whether table borders should collapse into a single border or remain separated as is the default.
- CSS – border-spacing PropertyThe border-spacing property sets the distance between the borders of adjacent table cells. This property only works when border-collapse is set to separate.
- CSS – caption-side PropertyThe caption-side property specifies the placement of a table caption to the top or bottom.
- CSS – empty-cells PropertyThe empty-cells property sets whether or not to display borders on empty cells in a table.
- CSS – table-layout PropertyThe table-layout property defines the algorithm used to layout table cells, rows, and columns.
CSS Notes:
- The “inherit”, “initial” and “unset” keywords can be used with any CSS property to set its value
- In CSS there are many ways to express a color value in a property
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.