Creating a Table
Tables are made up of data that is contained within columns and rows, and HTML supplies several different elements for defining and structuring these items. At a minimum a table must consist of
,<tr> (table row), and <td> (table data)
elements. For greater structure and additional semantic value, tables may include the <th> (table header)
ele- ment and a few other elements as well.Table
We use the <table>
element to initialize a table on a page.
Table Row
Once a table has been defined in HTML, table rows may be added using the <tr>
element.
Table Data
Once a table is defined and rows within that table have been set up, data cells may be added to the table via the table data, or <td>
, element.
Table Header
To designate a heading for a column or row of cells, the table header element, <th>
, should be used. The <th>
element works just like the <td>
element in that it creates a cell for data. The value to using the <th>
element over the <td>
element is that the table header element provides semantic value by signifying that the data within the cell is a heading, while the <td>
element only represents a generic piece of data.
Table Structure
Knowing how to build a table and arrange data is extremely powerful; however, there are a few additional elements to help us organize the data and structure of a table. These elements include <caption>, <thead>, <tbody>, and <tfoot>
.
Table Caption
The <caption>
element provides a caption or title for a table. A caption will help users identify what the table pertains to and what data they can expect to find within it. The <caption>
element must come immediately after the opening <table>
tag, and it is positioned at the top of a table by default.
Table Head, Body, & Foot
<thead> <tbody> or <tfoot>
Combining Multiple Cells
The colspan
attribute is used to span a single cell across multiple columns within a table, while the rowspan
attribute is used to span a single cell across multiple rows. Each attribute accepts an integer value that indicates the number of cells to span across, with 1 being the default value.
Table Borders
When styling table borders with CSS there are two properties that will quickly come in handy: border-collapse and border-spacing
.
Border Collapse Property
The border-collapse
property determines a table’s border model. There are three values for the border-collapse property: collapse, separate, and inherit
. By default, the border-collapse property value is separate
, meaning that all of the different borders will stack up next to one another, as described above. The collapse property, on the other hand, condenses the borders into one, choosing the table cell as the primary border.
table {
border-collapse: collapse;
}
th,
td {
border: 1px solid #cecfd5;
padding: 10px 15px;
}
Border Spacing Property
As the border-collapse
property with the separate
value allows borders to be stacked up against one another, the border-spacing property can determine how much space, if any, appears between the borders.
The border-spacing
property works only when the border-collapse
property value is separate
, its default value. If the border-collapse
property hasn’t been previously used, we can use the border-spacing
property without worry.
Adding Borders to Rows
Adding borders to a table can be tricky at times, particularly when putting borders between rows. Within a table, borders cannot be applied to <tr>
elements or table structural elements, so when we want to put a border between rows some thought is required.
We’ll begin by making sure the table’s border-collapse
property value is set to collapse
, and then we’ll add a bottom border to each table cell, regardless of whether it’s a <th> or <td>
element. If we wish, we can remove the bottom border
from the cells within the last row of the table by using the :last-child
pseudo-class selector to select the last <tr>
element within the table and target the <td>
elements within that row. Additionally, if a table is using the structural elements, we’ll want to make sure to prequalify the last row of the table as being within the <tfoot>
element.
table {
border-collapse: collapse;
}
th,
td {
border-bottom: 1px solid #cecfd5;
padding: 10px 15px;
}
tfoot tr:last-child td {
border-bottom: 0;
}
Table Striping
In the effort to make tables more legible, one common design practice is to “stripe” table rows with alternating background colors. This makes the rows clearer and provides a visual cue for scanning information. One way to do this is to place a class on every other <tr>
element and set a background color to that class. Another, easier way is to use the :nth-child
pseudo-class selector with an even or odd
argument to select every other <tr>
element.
table {
border-collapse: separate;
border-spacing: 0;
}
th,
td {
padding: 10px 15px;
}
thead {
background: #395870;
color: #fff;
}
tbody tr:nth-child(even) {
background: #f0f0f2;
}
td {
border-bottom: 1px solid #cecfd5;
border-right: 1px solid #cecfd5;
}
td:first-child {
border-left: 1px solid #cecfd5;
}
Aligning Text
To align text vertically, however, the vertical-align
property is used. The vertical-align
property works only with inline and table-cell elements—it won’t work for block, inline-block, or any other element levels.
The vertical-align
property accepts a handful of different values; the most popular values are top, middle, and bottom
. These values vertically position text in relation to the table cell, for table-cell elements, or to the closest parent element, for inline-level elements.
td {
padding: 10px 15px;
vertical-align: middle;
}