Sub topic 3
Responsive Images and Lists
Assume you know basic HTML documents, file paths, and how to open an HTML file in a browser. This chapter focuses on choosing suitable image files and structuring related content with lists.
Responsive images: `srcset`, `sizes`, and `picture`
Responsive images let the browser choose an image that fits the device and layout. srcset lists image candidates by width. sizes describes the image's displayed width, so the browser can choose efficiently.
Use picture when you need different formats or different image files, such as a cropped mobile image. The img element remains the fallback and must include useful alt text.
<picture>
<source srcset="images/hero.avif" type="image/avif">
<source srcset="images/hero.webp" type="image/webp">
<img
src="images/hero-800.jpg"
srcset="images/hero-400.jpg 400w, images/hero-800.jpg 800w, images/hero-1600.jpg 1600w"
sizes="(min-width: 900px) 800px, 100vw"
alt="Mountain lake at sunrise"
width="800"
height="450">
</picture>The browser selects the first supported picture source. For a normal img, sizes says the image occupies 800 pixels on wide screens and the full viewport width otherwise.
Unordered lists
Use ul when item order does not matter. Each item belongs in an li element.
<ul>
<li>Keyboard</li>
<li>Mouse</li>
<li>Monitor</li>
</ul>Ordered lists
Use ol when sequence matters. start changes the first number, and reversed counts downward.
| Element | Use |
|---|---|
| `ul` | Unordered items |
| `ol` | Ordered steps or rankings |
Nesting lists
Put a nested list inside the parent item's li, not directly inside the parent ul or ol.
<ul>
<li>Fruit
<ul>
<li>Apples</li>
<li>Pears</li>
</ul>
</li>
<li>Vegetables</li>
</ul>Description lists
Use dl for terms and their descriptions. Each term uses dt; its explanation uses dd.
<dl>
<dt>HTML</dt>
<dd>The language used to structure web content.</dd>
</dl>Building a navigation menu from a list
A navigation menu is commonly a nav containing a list of links. Use descriptive link text and mark the current page with aria-current.
<nav aria-label="Main">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/articles/">Articles</a></li>
<li><a href="/contact.html">Contact</a></li>
</ul>
</nav>Exercise
Which element should contain a nested list?
Show answer
The parent item's li element.