1. Introduction to Tables

There are many websites on the Internet that display information like stock prices, sports scores, invoice data, and more. This data is naturally tabular in nature, meaning that a table is often the best way of presenting the data.

In this part of the course, we’ll learn how to use the HTML <table> element to present information in a two-dimensional table to the users.

Let’s get started!


2. Create a Table

Before displaying data, we must first create the table that will contain the data by using the <table> element.

  1. <table>
  2. </table>

The <table> element will contain all of the tabular data we plan on displaying.


3. 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>.

  1. <table>
  2. <tr>
  3. </tr>
  4. <tr>
  5. </tr>
  6. </table>

In the example above, two rows have been added to the table.


4. 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>.

  1. <table>
  2. <tr>
  3. <td>73</td>
  4. <td>81</td>
  5. </tr>
  6. </table>

In the example above, two data points (73 and 81) were entered in the one row that exists. By adding two data points, we created two cells of data.

If the table were displayed in the browser, it would show a table with one row and two columns.


5. 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 t**able 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.

  1. <table>
  2. <tr>
  3. <th></th>
  4. <th scope="col">Saturday</th>
  5. <th scope="col">Sunday</th>
  6. </tr>
  7. <tr>
  8. <th scope="row">Temperature</th>
  9. <td>73</td>
  10. <td>81</td>
  11. </tr>
  12. </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.

Note, also, the use of the scope attribute, which can take one of two values:

  1. row - this value makes it clear that the heading is for a row.
  2. 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.


6. Table Borders

So far, the tables you’ve created have been a little difficult to read because they have no 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.

  1. <table border="1">
  2. <tr>
  3. <td>73</td>
  4. <td>81</td>
  5. </tr>
  6. </table>

The code in the example above is following is deprecated, so please don’t use it. It’s meant to illustrate older conventions you may come across when reading other developers’ code.

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.

  1. table, td {
  2. border: 1px solid black;
  3. }

The code in the example above uses CSS instead of HTML to show table borders.


7. 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 attributes accepts an integer (greater than or equal to 1) to denote the number of columns it spans across.

  1. <table>
  2. <tr>
  3. <th>Monday</th>
  4. <th>Tuesday</th>
  5. <th>Wednesday</th>
  6. </tr>
  7. <tr>
  8. <td colspan="2">Out of Town</td>
  9. <td>Back in Town</td>
  10. </tr>
  11. </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.


8. 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.

  1. <table>
  2. <tr> <!-- Row 1 -->
  3. <th></th>
  4. <th>Saturday</th>
  5. <th>Sunday</th>
  6. </tr>
  7. <tr> <!-- Row 2 -->
  8. <th>Morning</th>
  9. <td rowspan="2">Work</td>
  10. <td rowspan="3">Relax</td>
  11. </tr>
  12. <tr> <!-- Row 3 -->
  13. <th>Afternoon</th>
  14. </tr>
  15. <tr> <!-- Row 4 -->
  16. <th>Evening</th>
  17. <td>Dinner</td>
  18. </tr>
  19. </table>

In the example above, there are four rows:

  1. The first row contains an empty cell and the two column headings.
  2. The second row contains the Morning row heading, along with Work, which spans two rows under the Saturday column. The “Relax” entry spans three rows under the Sunday column.
  3. The third row only contains the Afternoon row heading.
  4. The fourth row only contains the Dinner entry, since “Relax” spans into the cell next to it.

If you’d like to see how the browser interprets the code above, feel free to copy and paste it into the code editor to understand it a little better.


9. 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).

  1. <table>
  2. <tbody>
  3. <tr>
  4. <th></th>
  5. <th>Saturday</th>
  6. <th>Sunday</th>
  7. </tr>
  8. <tr>
  9. <th>Morning</th>
  10. <td rowspan="2">Work</td>
  11. <td rowspan="3">Relax</td>
  12. </tr>
  13. <tr>
  14. <th>Afternoon</th>
  15. </tr>
  16. <tr>
  17. <th>Evening</th>
  18. <td>Dinner</td>
  19. </tr>
  20. </tbody>
  21. </table>

In the example above, all of the table data is contained within a table body element. Note, however, that the headings were also kept in the table’s body — we’ll change this in the next exercise.


10. 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.

  1. <table>
  2. <thead>
  3. <tr>
  4. <th></th>
  5. <th scope="col">Saturday</th>
  6. <th scope="col">Sunday</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <tr>
  11. <th scope="row">Morning</th>
  12. <td rowspan="2">Work</td>
  13. <td rowspan="3">Relax</td>
  14. </tr>
  15. <tr>
  16. <th scope="row">Afternoon</th>
  17. </tr>
  18. <tr>
  19. <th scope="row">Evening</th>
  20. <td>Dinner</td>
  21. </tr>
  22. </tbody>
  23. </table>

In the example above, the only new element is <thead>. The table headings are contained inside of this element. Note that the table’s head still requires a row in order to contain the table headings.

Additionally, only the column headings go under the <thead> element. We can use the scope attribute on <th> elements to indicate whether a <th> element is being used as a "row" heading or a "col" heading.


11. Table Footer

The bottom part of a long table can also be sectioned off using the <tfoot> element.

  1. <table>
  2. <thead>
  3. <tr>
  4. <th>Quarter</th>
  5. <th>Revenue</th>
  6. <th>Costs</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <tr>
  11. <th>Q1</th>
  12. <td>$10M</td>
  13. <td>$7.5M</td>
  14. </tr>
  15. <tr>
  16. <th>Q2</th>
  17. <td>$12M</td>
  18. <td>$5M</td>
  19. </tr>
  20. </tbody>
  21. <tfoot>
  22. <tr>
  23. <th>Total</th>
  24. <td>$22M</td>
  25. <td>$12.5M</td>
  26. </tr>
  27. </tfoot>
  28. </table>

In the example above, the footer contains the totals of the data in the table. Footers are often used to contain sums, differences, and other data results.


12. 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.

You can use CSS to style tables. Specifically, you can style the various aspects mentioned above.

  1. table, th, td {
  2. border: 1px solid black;
  3. font-family: Arial, sans-serif;
  4. text-align: center;
  5. }

The code in the example above demonstrates just some of the various table aspects you can style using CSS properties.


13. Review

Great job! In this lesson, we learned how to create a table, add data to it, and section the table into smaller parts that make it easier to read.

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.

Congratulations on completing HTML Tables!