There are many websites on the Internet that display information like stock prices, sports scores, invoice data, and more. This data is tabular in nature, meaning that a table is often the best way of presenting the data.
we’ll learn how to use the HTML <table>
element to present information in a two-dimensional table to the users.
Create a Table
Before displaying data, we must first create the table that will contain the data by using the <table>
element.
<table>
</table>
Table Rows
In many programs that use tables, the table is already predefined for you, meaning that it contains the rows, columns, and cells that will hold data. In HTML, all of these components must be created.
The first step in entering data into the table is to add rows using the table row element: <tr>
.
<table>
<tr>
</tr>
<tr>
</tr>
</table>
Table Data
Rows aren’t sufficient to add data to a table. Each cell element must also be defined. In HTML, you can add data using the table data element: <td>
.
<table>
<tr>
<td>73</td>
<td>81</td>
</tr>
</table>
Table Headings
Table data doesn’t make much sense without titles to describe what the data represents.
To add titles to rows and columns, you can use the table heading element: <th>
.
The table heading element is used just like a table data element, except with a relevant title. Just like table data, a table heading must be placed within a table row.
<table>
<tr>
<th></th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
<tr>
<th scope="row">Temperature</th>
<td>73</td>
<td>81</td>
</tr>
</table>
What happened in the code above?
First, a new row was added to hold the three headings: a blank heading, a Saturday
heading, and a Sunday
heading. The blank heading creates the extra table cell necessary to align the table headings correctly over the data they correspond to.
In the second row, one table heading was added as a row title: Temperature
.
When rendered, this code will appear similar to the image below:
Note, also, the use of the scope
attribute, which can take one of two values:
row
- this value makes it clear that the heading is for a row.col
- this value makes it clear that the heading is for a column.
HTML code for tables may look a little strange at first, but analyzing it piece by piece helps make the code more understandable.
Table Borders
In older versions of HTML, a border could be added to a table using the border
attribute and setting it equal to an integer. This integer would represent the thickness of the border.
<table border="1">
<tr>
<td>73</td>
<td>81</td>
</tr>
</table>
The browser will likely still interpret your code correctly if you use the border
attribute, but that doesn’t mean the attribute should be used.
We use CSS to add style to HTML documents, because it helps us to separate the structure of a page from how it looks. You can learn more about CSS in our CSS courses.
You can achieve the same table border effect using CSS.
table, td {
border: 1px solid black;
}
The code in the example above uses CSS instead of HTML to show table borders.
<!DOCTYPE html>
<html>
<head>
<title>Ship To It - Company Packing List</title>
<link href="https://fonts.googleapis.com/css?family=Lato: 100,300,400,700|Luckiest+Guy|Oxygen:300,400" rel="stylesheet">
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<ul class="navigation">
<li><img src="https://content.codecademy.com/courses/web-101/unit-9/htmlcss1-img_logo-shiptoit.png" height="20px;"></li>
<li class="active">Action List</li>
<li>Profiles</li>
<li>Settings</li>
</ul>
<div class="search">Search the table</div>
<table>
<tr>
<th scope="col">Company Name</th>
<th scope="col">Number of Items to Ship</th>
<th scope="col">Next Action</th>
</tr>
<tr>
<td>Adam’s Greenworks</td>
<td>14</td>
<td>Package Items</td>
</tr>
<tr>
<td>Davie's Burgers</td>
<td>2</td>
<td>Send Invoice</td>
</tr>
<tr>
<td>Baker's Bike Shop</td>
<td>3</td>
<td>Send Invoice</td>
</tr>
<tr>
<td>Miss Sally's Southern</td>
<td>4</td>
<td>Ship</td>
</tr>
<tr>
<td>Summit Resort Rentals</td>
<td>4</td>
<td>Ship</td>
</tr>
<tr>
<td>Strike Fitness</td>
<td>1</td>
<td>Enter Order</td>
</tr>
</table>
</body>
</html>
Spanning Columns
What if the table contains data that spans multiple columns?
For example, a personal calendar could have events that span across multiple hours, or even multiple days.
Data can span columns using the colspan
attribute. The attribute accepts an integer (greater than or equal to 1) to denote the number of columns it spans across.
<table>
<tr>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
</tr>
<tr>
<td colspan="2">Out of Town</td>
<td>Back in Town</td>
</tr>
</table>
In the example above, the data Out of Town
spans the Monday
and Tuesday
table headings using the value 2
(two columns). The data Back in Town
appear only under the Wednesday
heading.
Spanning Rows
Data can also span multiple rows using the rowspan
attribute.
The rowspan
attribute is used for data that spans multiple rows (perhaps an event goes on for multiple hours on a certain day). It accepts an integer (greater than or equal to 1) to denote the number of rows it spans across.
<table>
<tr> <!-- Row 1 -->
<th></th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<tr> <!-- Row 2 -->
<th>Morning</th>
<td rowspan="2">Work</td>
<td rowspan="3">Relax</td>
</tr>
<tr> <!-- Row 3 -->
<th>Afternoon</th>
</tr>
<tr> <!-- Row 4 -->
<th>Evening</th>
<td>Dinner</td>
</tr>
</table>
Table Body
Over time, a table can grow to contain a lot of data and become very long. When this happens, the table can be sectioned off so that it is easier to manage.
Long tables can be sectioned off using the table body element: <tbody>
.
The <tbody>
element should contain all of the table’s data, excluding the table headings (more on this in a later exercise).
<table>
<tbody>
<tr>
<th></th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<tr>
<th>Morning</th>
<td rowspan="2">Work</td>
<td rowspan="3">Relax</td>
</tr>
<tr>
<th>Afternoon</th>
</tr>
<tr>
<th>Evening</th>
<td>Dinner</td>
</tr>
</tbody>
</table>
Table Head
In the last exercise, the table’s headings were kept inside of the table’s body. When a table’s body is sectioned off, however, it also makes sense to section off the table’s column headings using the <thead>
element.
table>
<thead>
<tr>
<th></th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Morning</th>
<td rowspan="2">Work</td>
<td rowspan="3">Relax</td>
</tr>
<tr>
<th scope="row">Afternoon</th>
</tr>
<tr>
<th scope="row">Evening</th>
<td>Dinner</td>
</tr>
</tbody>
</table>
Table Footer
The bottom part of a long table can also be sectioned off using the <tfoot>
element.
<table>
<thead>
<tr>
<th>Quarter</th>
<th>Revenue</th>
<th>Costs</th>
</tr>
</thead>
<tbody>
<tr>
<th>Q1</th>
<td>$10M</td>
<td>$7.5M</td>
</tr>
<tr>
<th>Q2</th>
<td>$12M</td>
<td>$5M</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Total</th>
<td>$22M</td>
<td>$12.5M</td>
</tr>
</tfoot>
</table>
Styling with CSS
Tables, by default, are very bland. They have no borders, the font color is black, and the typeface is the same type used for other HTML elements.
CSS, or Cascading Style Sheets, is a language that web developers use to style the HTML content on a web page. You can use CSS to style tables. Specifically, you can style the various aspects mentioned above.
table, th, td {
border: 1px solid black;
font-family: Arial, sans-serif;
text-align: center;
}
body {
background: #EEE;
margin: 0;
padding: 0;
}
/* Navigation */
.navigation {
box-sizing: border-box;
background-color: #3587A4;
overflow: auto;
padding: 18px 50px;
position: relative;
top: 0;
width: 100%;
z-index: 999;
}
ul {
padding: 0;
margin: 0;
}
li {
color: #FFF;
display: inline-block;
font-family: 'Oxygen', sans-serif;
font-size: 16px;
font-weight: 300;
letter-spacing: 2px;
margin: 0;
padding: 20px 18px 10px 18px;
text-transform: uppercase;
}
.active {
color: #88CCF1;
}
/* Table */
table {
height: 40%;
left: 10%;
margin: 20px auto;
overflow-y: scroll;
position: static;
width: 80%;
}
thead th {
background: #88CCF1;
color: #FFF;
font-family: 'Lato', sans-serif;
font-size: 16px;
font-weight: 100;
letter-spacing: 2px;
text-transform: uppercase;
}
tr {
background: #f4f7f8;
border-bottom: 1px solid #FFF;
margin-bottom: 5px;
}
th, td {
font-family: 'Lato', sans-serif;
font-weight: 400;
font-size: 18px;
padding: 20px;
text-align: left;
width: 33.3333%;
}
.search {
background-color: #FFF;
border: 1px solid #DDD;
border-radius: 3px;
color: #AAA;
padding: 20px;
margin: 50px auto 0px auto;
width: 77%;
}
Let’s review what we’ve learned so far:
- The
<table>
element creates a table. - The
<tr>
element adds rows to a table. - To add data to a row, you can use the
<td>
element. - Table headings clarify the meaning of data. Headings are added with the
<th>
element. - Table data can span columns using the
colspan
attribute. - Table data can span rows using the
rowspan
attribute. - Tables can be split into three main sections: a head, a body, and a footer.
- A table’s head is created with the
<thead>
element. - A table’s body is created with the
<tbody>
element. - A table’s footer is created with the
<tfoot>
element. - All the CSS properties you learned about in this course can be applied to tables and their data.