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
@@ -178,22 +178,115 @@ These elements don't necessarily make the table any more accessible to screen re
178
178
To use them, they should be included in the following order:
179
179
180
180
- The `<thead>` element must wrap the part of the table that is the header — this is usually the first row containing the column headings, but this is not necessarily always the case. If you are using {{htmlelement("col")}}/{{htmlelement("colgroup")}} elements, the table header should come just below those.
181
-
- The `<tbody>` element needs to wrap the main part of the table content that isn't the table header or footer.
182
-
- The `<tfoot>` element needs to wrap the part of the table that is the footer — this might be a final row with items in the previous rows summed, for example.
181
+
- The `<tbody>` element needs to wrap the main part of the table content that isn't the table header or footer, and should come after the `<thead>`.
182
+
- The `<tfoot>` element needs to wrap the part of the table that is the footer — this might be a final row with items in the previous rows summed, for example. The `<tfoot>` should come after the `<tbody>`.
183
183
184
184
> [!NOTE]
185
-
> `<tbody>` is always included in every table, implicitly if you don't specify it in your code. To check this, open up one of your previous examples that doesn't include `<tbody>` and look at the HTML code in your [browser developer tools](/en-US/docs/Learn_web_development/Howto/Tools_and_setup/What_are_browser_developer_tools) — you will see that the browser has added this tag for you. You might wonder why you ought to bother including it at all — you should, because it gives you more control over your table structure and styling.
185
+
> `<tbody>` is always implicitly included in every table if you don't specify it in your code. To check this, open up one of your previous examples that doesn't include `<tbody>` and look at the HTML code in your [browser developer tools](/en-US/docs/Learn_web_development/Howto/Tools_and_setup/What_are_browser_developer_tools) — you will see that the browser has added this tag for you. You might wonder why you ought to bother including it at all — you should, because it gives you more control over your table structure and styling.
186
186
187
187
### Adding structure to a spending record table
188
188
189
189
Let's get you to put these new elements into action.
190
190
191
-
1. First of all, make a local copy of [spending-record.html](https://github.com/mdn/learning-area/blob/main/html/tables/advanced/spending-record.html) and [minimal-table.css](https://github.com/mdn/learning-area/blob/main/html/tables/advanced/minimal-table.css) in a new folder.
192
-
2. Try putting the obvious headers row inside a `<thead>` element, the "SUM" row inside a `<tfoot>` element, and the rest of the content inside a `<tbody>` element.
193
-
3. Next, add a [`colspan`](/en-US/docs/Web/HTML/Reference/Elements/td#colspan) attribute to make the "SUM" cell span across the first four columns, so the actual number appears at the bottom of the "Cost" column.
194
-
4. Let's add some simple extra styling to the table, to give you an idea of how useful these elements are for applying CSS. Inside the head of your HTML document, you'll see an empty {{htmlelement("style")}} element. Inside this element, add the following lines of CSS code:
191
+
1. First of all, create a new HTML file called `spending-record.html` and put the following HTML inside the `<body>`:
192
+
193
+
```html
194
+
<h1>My spending record</h1>
195
+
196
+
<table>
197
+
<caption>
198
+
How I chose to spend my money
199
+
</caption>
200
+
<tr>
201
+
<th>Purchase</th>
202
+
<th>Location</th>
203
+
<th>Date</th>
204
+
<th>Evaluation</th>
205
+
<th>Cost (€)</th>
206
+
</tr>
207
+
<tr>
208
+
<td>Haircut</td>
209
+
<td>Hairdresser</td>
210
+
<td>12/09</td>
211
+
<td>Great idea</td>
212
+
<td>30</td>
213
+
</tr>
214
+
<tr>
215
+
<td>Lasagna</td>
216
+
<td>Restaurant</td>
217
+
<td>12/09</td>
218
+
<td>Regrets</td>
219
+
<td>18</td>
220
+
</tr>
221
+
<tr>
222
+
<td>Shoes</td>
223
+
<td>Shoeshop</td>
224
+
<td>13/09</td>
225
+
<td>Big regrets</td>
226
+
<td>65</td>
227
+
</tr>
228
+
<tr>
229
+
<td>Toothpaste</td>
230
+
<td>Supermarket</td>
231
+
<td>13/09</td>
232
+
<td>Good</td>
233
+
<td>5</td>
234
+
</tr>
235
+
<tr>
236
+
<td>SUM</td>
237
+
<td>118</td>
238
+
</tr>
239
+
</table>
240
+
```
241
+
242
+
2. Next, create a CSS file called `minimal-table.css` in the same directory as your HTML file and fill it with the following content:
243
+
244
+
```css live-sample___finished-table-structure
245
+
html {
246
+
font-family: sans-serif;
247
+
}
248
+
249
+
table {
250
+
border-collapse: collapse;
251
+
border: 2pxsolidrgb(200200200);
252
+
letter-spacing: 1px;
253
+
font-size: 0.8rem;
254
+
}
255
+
256
+
td,
257
+
th {
258
+
border: 1pxsolidrgb(190190190);
259
+
padding: 10px20px;
260
+
}
261
+
262
+
th {
263
+
background-color: rgb(235235235);
264
+
}
265
+
266
+
td {
267
+
text-align: center;
268
+
}
269
+
270
+
tr:nth-child(even) td {
271
+
background-color: rgb(250250250);
272
+
}
273
+
274
+
tr:nth-child(odd) td {
275
+
background-color: rgb(245245245);
276
+
}
277
+
278
+
caption {
279
+
padding: 10px;
280
+
}
281
+
```
195
282
196
-
```css
283
+
3. Add a `<link>` element into your HTML `<head>` to apply the CSS to the HTML (see [Applying CSS and JavaScript to HTML](/en-US/docs/Learn_web_development/Core/Structuring_content/Webpage_metadata#applying_css_and_javascript_to_html) for help with this).
284
+
285
+
4. Try putting the obvious headers row inside a `<thead>` element, the "SUM" row inside a `<tfoot>` element, and the rest of the content inside a `<tbody>` element.
286
+
5. Next, add a [`colspan`](/en-US/docs/Web/HTML/Reference/Elements/td#colspan) attribute to make the "SUM" cell span across the first four columns, so the actual number appears at the bottom of the "Cost" column.
287
+
6. Let's add some simple extra styling to the table, to give you an idea of how useful these elements are for applying CSS. Add the following to your CSS file:
288
+
289
+
```css live-sample___finished-table-structure
197
290
tbody {
198
291
font-size: 95%;
199
292
font-style: italic;
@@ -207,14 +300,18 @@ Let's get you to put these new elements into action.
207
300
> [!NOTE]
208
301
> We don't expect you to fully understand the CSS right now. You'll learn more about this when you go through our CSS modules (starting with [CSS Styling basics](/en-US/docs/Learn_web_development/Core/Styling_basics), which includes an article specifically on [styling tables](/en-US/docs/Learn_web_development/Core/Styling_basics/Tables)).
209
302
210
-
5. Save and refresh, and have a look at the result. If the `<tbody>` and `<tfoot>` elements weren't in place, you'd have to write much more complicated selectors/rules to apply the same styling.
303
+
7. Save and refresh, and have a look at the result. If the `<tbody>` and `<tfoot>` elements weren't in place, you'd have to write much more complicated selectors/rules to apply the same styling.
<summary>Click here to show the solution</summary>
214
311
215
312
Your finished HTML should look something like this:
216
313
217
-
```html
314
+
```html live-sample___finished-table-structure
218
315
<table>
219
316
<caption>
220
317
How I chose to spend my money
@@ -228,12 +325,6 @@ Your finished HTML should look something like this:
228
325
<th>Cost (€)</th>
229
326
</tr>
230
327
</thead>
231
-
<tfoot>
232
-
<tr>
233
-
<tdcolspan="4">SUM</td>
234
-
<td>118</td>
235
-
</tr>
236
-
</tfoot>
237
328
<tbody>
238
329
<tr>
239
330
<td>Haircut</td>
@@ -264,11 +355,15 @@ Your finished HTML should look something like this:
264
355
<td>5</td>
265
356
</tr>
266
357
</tbody>
358
+
<tfoot>
359
+
<tr>
360
+
<tdcolspan="4">SUM</td>
361
+
<td>118</td>
362
+
</tr>
363
+
</tfoot>
267
364
</table>
268
365
```
269
366
270
-
You can find the complete code on GitHub at [spending-record-finished.html](https://github.com/mdn/learning-area/blob/main/html/tables/advanced/spending-record-finished.html) ([see it running live also](https://mdn.github.io/learning-area/html/tables/advanced/spending-record-finished.html)).
0 commit comments