18. Creating HTML tables
Module 4 · About 20 minutes · Lesson 17; review earlier concepts as needed
Open Interactive LessonDiscover
What changes when you use creating html tables, and how can you prove it?
- Explain creating html tables using the example source
- Predict the visual or structural effect of one controlled change
- Repair the supplied fault and verify actual output
Predict
Record the expected structure or visual change before rendering. Name one thing that should remain unchanged.
Understand
Tables express relationships between rows and columns. They are not a layout shortcut for entire pages. tr groups a row; th identifies a header cell and td a data cell. The browser may insert tbody when parsing. Check the DOM and use a caption to explain what the numbers represent. For narrow screens, allow a meaningful table to scroll rather than destroying its relationships.
Important source lines
- index.html:1 <table><caption>Workshop prices</caption><thead><tr><th scope="col">Session</th><th scope="col">Price</th></tr></thead><tbody><tr><th scope="row">Basics</th><td>$30</td></tr></tbody></table>
- Tabular relationships between data in rows and columns. Attribute guidance: use caption and associated headers as needed.
- styles.css:1 th, td { border: 1px solid #9ba8b8; padding: .5rem; }
- Combines border width, style and color. Accepted grammar: <line-width> || <line-style> || <color>. Context: block.
The first example teaches this lesson’s primary contract. Additional examples are optional related references or applications, each with its own purpose and source; an example without an authored check requires the stated visual/manual review.
Example 1: Creating HTML tables
The price belongs to a tabular data cell aligned with its session and column header.
index.html
<table><caption>Workshop prices</caption><thead><tr><th scope="col">Session</th><th scope="col">Price</th></tr></thead><tbody><tr><th scope="row">Basics</th><td>$30</td></tr></tbody></table>
styles.css
th, td { border: 1px solid #9ba8b8; padding: .5rem; }script.js
Expected: The price belongs to a tabular data cell aligned with its session and column header.
Edit and Render This ExampleExample 2: Element: table
Tabular relationships between data in rows and columns.
index.html
<table><caption>Sessions</caption><tr><th scope="col">Topic</th><th scope="col">Seats</th></tr><tr><th scope="row">Grid</th><td>12</td></tr></table>
styles.css
body { font:18px/1.6 system-ui; margin:1.5rem; color:#17324d; } img,video,svg { max-width:100%; height:auto; } th,td { padding:.5rem; border:1px solid #a8b8c8; } :focus-visible { outline:3px solid #a44322; outline-offset:3px; }script.js
Expected: Tabular relationships between data in rows and columns. Review its real DOM and native behavior; structural/media-only examples are labeled.
Edit and Render This ExampleExample 3: Element: caption
A table’s visible name or description.
index.html
<table><caption>Monthly attendance</caption><tr><th scope="col">Month</th><th scope="col">People</th></tr><tr><td>June</td><td>24</td></tr></table>
styles.css
body { font:18px/1.6 system-ui; margin:1.5rem; color:#17324d; } img,video,svg { max-width:100%; height:auto; } th,td { padding:.5rem; border:1px solid #a8b8c8; } :focus-visible { outline:3px solid #a44322; outline-offset:3px; }script.js
Expected: A table’s visible name or description. Review its real DOM and native behavior; structural/media-only examples are labeled.
Edit and Render This ExampleExample 4: Element: thead
A group of table header rows.
index.html
<table><thead><tr><th scope="col">Name</th><th scope="col">Status</th></tr></thead><tbody><tr><td>Alex</td><td>Ready</td></tr></tbody></table>
styles.css
body { font:18px/1.6 system-ui; margin:1.5rem; color:#17324d; } img,video,svg { max-width:100%; height:auto; } th,td { padding:.5rem; border:1px solid #a8b8c8; } :focus-visible { outline:3px solid #a44322; outline-offset:3px; }script.js
Expected: A group of table header rows. Review its real DOM and native behavior; structural/media-only examples are labeled.
Edit and Render This ExampleExample 5: Element: tr
A table row containing header/data cells.
index.html
<table><tr><th scope="row">Monday</th><td>12 seats</td></tr></table>
styles.css
body { font:18px/1.6 system-ui; margin:1.5rem; color:#17324d; } img,video,svg { max-width:100%; height:auto; } th,td { padding:.5rem; border:1px solid #a8b8c8; } :focus-visible { outline:3px solid #a44322; outline-offset:3px; }script.js
Expected: A table row containing header/data cells. Review its real DOM and native behavior; structural/media-only examples are labeled.
Edit and Render This ExampleBuild and test
The price belongs to a tabular data cell aligned with its session and column header. Repair the current exercise without removing necessary content. Checks inspect the stated structural/computed/stylesheet contract, not exact source text.
Starter index.html
<table><caption>Workshop prices</caption><thead><tr><th scope="col">Session</th><th scope="col">Price</th></tr></thead><tbody><tr><th scope="row">Basics</th><span>$30</span></tr></tbody></table>
Starter styles.css
th, td { border: 1px solid #9ba8b8; padding: .5rem; }Objective contract
[
{
"type": "count",
"selector": "table td",
"value": 1,
"label": "Include 1 matching table td element(s)"
}
]Hint 1
Locate the first difference between the intended result and the real preview. Inspect matching elements and computed styles.
Hint 2
The relevant source uses <td>$30</td>. The exercise instead uses <span>$30</span>. Identify its effect before changing it.
Hint 3
Restore the intended <td>$30</td> operation or implement an equivalent structure/style. Run the actual checks and explain why it works.
Complete reference solution
index.html
<table><caption>Workshop prices</caption><thead><tr><th scope="col">Session</th><th scope="col">Price</th></tr></thead><tbody><tr><th scope="row">Basics</th><td>$30</td></tr></tbody></table>
styles.css
th, td { border: 1px solid #9ba8b8; padding: .5rem; }Explain, reflect and extend
Which part of the actual preview or inspector proves this result? Explain one boundary case and name what automated checks do not verify.
Apply this concept to a different realistic piece of content. Predict its structure or visual behavior, test narrow and wide widths where relevant, then record one invalid case and its repair.
Troubleshooting and professional practice
If nothing changes, confirm that you edited the active file and ran Run Preview, or enabled Auto Preview. Inspect the actual DOM because the browser may repair invalid nesting. For CSS, check selector matches, valid declarations, active queries and computed values. Preserve local asset paths and inspect warnings. The price belongs to a tabular data cell aligned with its session and column header.
Knowledge check
Explain why the stated result follows from the source. Predict a second input or width and test it independently. Automated contracts and written explanations are reviewed separately.
Optional AI prompts
- Explain this lesson’s source line by line using the actual objective above.
- Help implement one small extension and define normal, boundary and failure tests.
- Review/debug the source against actual preview observations; verify standards and never claim rendering without evidence.