Skip to content

Commit c37a7ff

Browse files
Fix incorrect tfoot placement in Learn (mdn#41625)
* Fix incorrect tfoot placement in Learn * Update files/en-us/learn_web_development/core/structuring_content/table_accessibility/index.md Co-authored-by: Joshua Chen <sidachen2003@gmail.com> * Update files/en-us/learn_web_development/core/structuring_content/table_accessibility/index.md Co-authored-by: Joshua Chen <sidachen2003@gmail.com> * Update files/en-us/learn_web_development/core/structuring_content/table_accessibility/index.md Co-authored-by: Joshua Chen <sidachen2003@gmail.com> * Update files/en-us/learn_web_development/core/structuring_content/table_accessibility/index.md Co-authored-by: Joshua Chen <sidachen2003@gmail.com> * Update files/en-us/learn_web_development/core/structuring_content/table_accessibility/index.md Co-authored-by: Joshua Chen <sidachen2003@gmail.com> * Update files/en-us/learn_web_development/core/structuring_content/table_accessibility/index.md Co-authored-by: Joshua Chen <sidachen2003@gmail.com> * Fix border color syntax in table styles --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 2eabd83 commit c37a7ff

File tree

1 file changed

+113
-18
lines changed
  • files/en-us/learn_web_development/core/structuring_content/table_accessibility

1 file changed

+113
-18
lines changed

files/en-us/learn_web_development/core/structuring_content/table_accessibility/index.md

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,22 +178,115 @@ These elements don't necessarily make the table any more accessible to screen re
178178
To use them, they should be included in the following order:
179179

180180
- 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>`.
183183

184184
> [!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.
186186
187187
### Adding structure to a spending record table
188188

189189
Let's get you to put these new elements into action.
190190

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: 2px solid rgb(200 200 200);
252+
letter-spacing: 1px;
253+
font-size: 0.8rem;
254+
}
255+
256+
td,
257+
th {
258+
border: 1px solid rgb(190 190 190);
259+
padding: 10px 20px;
260+
}
261+
262+
th {
263+
background-color: rgb(235 235 235);
264+
}
265+
266+
td {
267+
text-align: center;
268+
}
269+
270+
tr:nth-child(even) td {
271+
background-color: rgb(250 250 250);
272+
}
273+
274+
tr:nth-child(odd) td {
275+
background-color: rgb(245 245 245);
276+
}
277+
278+
caption {
279+
padding: 10px;
280+
}
281+
```
195282

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
197290
tbody {
198291
font-size: 95%;
199292
font-style: italic;
@@ -207,14 +300,18 @@ Let's get you to put these new elements into action.
207300
> [!NOTE]
208301
> 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)).
209302
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.
304+
305+
The finished example should look like this:
306+
307+
{{embedlivesample("finished-table-structure", "100%", "300")}}
211308

212309
<details>
213310
<summary>Click here to show the solution</summary>
214311

215312
Your finished HTML should look something like this:
216313

217-
```html
314+
```html live-sample___finished-table-structure
218315
<table>
219316
<caption>
220317
How I chose to spend my money
@@ -228,12 +325,6 @@ Your finished HTML should look something like this:
228325
<th>Cost (€)</th>
229326
</tr>
230327
</thead>
231-
<tfoot>
232-
<tr>
233-
<td colspan="4">SUM</td>
234-
<td>118</td>
235-
</tr>
236-
</tfoot>
237328
<tbody>
238329
<tr>
239330
<td>Haircut</td>
@@ -264,11 +355,15 @@ Your finished HTML should look something like this:
264355
<td>5</td>
265356
</tr>
266357
</tbody>
358+
<tfoot>
359+
<tr>
360+
<td colspan="4">SUM</td>
361+
<td>118</td>
362+
</tr>
363+
</tfoot>
267364
</table>
268365
```
269366

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)).
271-
272367
</details>
273368

274369
## The `scope` attribute

0 commit comments

Comments
 (0)