You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: curriculum/challenges/english/25-front-end-development/lecture-working-with-tables/672a4e04a24858778f57ebfe.md
Watch the video lecture and answer the questions below.
12
12
13
+
# --transcript--
14
+
15
+
What are HTML tables used for, and what should they not be used for?
16
+
17
+
HTML tables aren't used as much these days as they used to be. But, as a frontend developer, you should still be familiar with them. Tables are one of the earliest ways devs had for displaying data in a browser way back in the 1990s.
18
+
19
+
Here's an example of code used to generate a table from the U.S. Bureau of Labor Statistics:
20
+
21
+
```html
22
+
<tableid="quickfacts">
23
+
<thead>
24
+
<tr>
25
+
<thcolspan="2">Quick Facts: Software Developers, Quality Assurance Analysts, and Testers</th>
26
+
</tr>
27
+
</thead>
28
+
<tbody>
29
+
<tr>
30
+
<th>2023 Median Pay</th>
31
+
<td>
32
+
$130,160 per year
33
+
<br>$62.58 per hour
34
+
</td>
35
+
</tr>
36
+
<tr>
37
+
<th>Typical Entry-Level Education</th>
38
+
<td>Bachelor's degree</td>
39
+
</tr>
40
+
<tr>
41
+
<th>Work Experience in a Related Occupation</th>
42
+
<td>None</td>
43
+
</tr>
44
+
<tr>
45
+
<th>On-the-job Training</th>
46
+
<td>None</td>
47
+
</tr>
48
+
<tr>
49
+
<th>Number of Jobs, 2022</th>
50
+
<td>1,795,300</td>
51
+
</tr>
52
+
<tr>
53
+
<th>Job Outlook, 2022-32</th>
54
+
<td>25% (Much faster than average)</td>
55
+
</tr>
56
+
<tr>
57
+
<th>Employment Change, 2022-32</th>
58
+
<td>451,200</td>
59
+
</tr>
60
+
</tbody>
61
+
<tfoot>
62
+
<tr>
63
+
<th>If this table had a footer it would be here.</th>
64
+
</tr>
65
+
</tfoot>
66
+
</table>
67
+
```
68
+
69
+
As you can see, there's a main `table` element with an `id` set to `quickfacts`. Inside it, the table has a table head element, `thead`, table body element, `tbody`, and a table foot element, `tfoot`.
70
+
71
+
The table head, body, and foot elements can each contain some number of table rows, `tr`. And each table row can contain a table header `th` which labels the data in that row. It can also contain some number of individual data cells, called table data, `td`.
72
+
73
+
Now, that's a lot of HTML elements. But don't be intimidated – these follow a simple hierarchy.
74
+
75
+
Here's the simplest table we can create that includes all of these elements:
76
+
77
+
```html
78
+
<table>
79
+
<thead>
80
+
<tr>
81
+
<th>The title of this table</th>
82
+
</tr>
83
+
</thead>
84
+
<tbody>
85
+
<tr>
86
+
<th>First Row</th>
87
+
<td>
88
+
First Data Cell
89
+
</td>
90
+
</tr>
91
+
<tr>
92
+
<th>Second Row</th>
93
+
<td>
94
+
Second Data Cell
95
+
</td>
96
+
</tr>
97
+
</tbody>
98
+
<tfoot>
99
+
<tr>
100
+
<th>The footer of this table, which might contain date of publication, author credits, or other meta information.</th>
101
+
</tr>
102
+
</tfoot>
103
+
</table>
104
+
```
105
+
106
+
So as you can see, the data itself is always within a `tr` – and within that `tr` element is a `th` element with a header, and a `td` element with data.
107
+
108
+
Some websites will choose to use `div`s to build their own tables instead of using the more appropriate `table` element.
109
+
110
+
While it is possible to display tabular data using generic `div` elements, it is still better to use the `table` element instead.
111
+
112
+
Many years ago, developers might have used a `table` to position non-data elements on a webpage. This was never considered a best practice. But you may encounter some codebases where tables are still used like this.
113
+
114
+
Nowadays, developers use CSS flexbox and grid to layout their designs. freeCodeCamp will cover these tools in depth later.
115
+
116
+
For now, just use HTML tables for their original intended purpose: displaying tabular data.
0 commit comments