Sub topic 3
Images and the `alt` Attribute
Assume you can create an HTML document and understand elements, attributes, and nesting. This chapter focuses on images, page structure, tables, forms, and basic validation.
Images and `alt`
The img element embeds an image. It is a void element, so it has no closing tag. The src attribute gives the image URL. The alt attribute gives equivalent text for people who cannot see the image.
<img src="trail-map.jpg"
alt="Map showing the north trail from the visitor center to the lake"
width="800"
height="500">Use a meaningful description when the image provides information. Use alt="" when an image is purely decorative. Do not repeat nearby text or use phrases such as “image of” unless that detail matters.
Semantic Layout Elements
Semantic elements describe a page’s purpose. They help browsers, assistive technology, and developers understand the structure.
Use main once for the page’s primary content. Use article for content that could stand alone, section for a themed group, and aside for related content. Semantic elements do not automatically create visual layouts; CSS controls appearance.
Tables for Tabular Data
Use a table when data has meaningful rows and columns. Do not use tables to position page elements.
<table>
<caption>Trail conditions</caption>
<thead>
<tr>
<th scope="col">Trail</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>North trail</td>
<td>Open</td>
</tr>
</tbody>
</table>caption names the table. Header cells with scope help assistive technology associate labels with data cells.
| Element | Purpose |
|---|---|
| `th` | Identifies a row or column heading |
| `td` | Holds a data value |
A First Look at Form Controls
A label identifies a control. Connect it to an input by matching for and id.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>
<button type="submit">Subscribe</button>
</form>The name attribute identifies submitted data. The required attribute enables basic browser validation.
Accessibility and Validation
Set the document language with <html lang="en">. Keep headings in a logical order, provide useful image alternatives, associate every form control with a label, and use semantic elements where they fit.
Check the page with an HTML validator. Then inspect the browser’s developer tools for missing resources, malformed markup, and console errors. Fix errors rather than relying on browsers to repair invalid nesting.
Exercise
What should the alt value be for a decorative divider image?
Show answer
Use alt="" because the image adds no information.