Learn HTML and CSS: Images
Images
"A website about ice cream without any ice cream on it?" Maggie sighs and puts a pile of drawings on the counter: a cone, a cup and a waffle.
The img element
You put an image on the page with <img>. Like <br>, it's an empty element: no content, no closing tag.
Which image it is comes from the attribute src, short for "source": the name of the file, relative just like a link.
The attribute alt describes what's in the picture. People who have the page read aloud hear that text. And if the image doesn't load, the text appears in its place.
<main> <h1>Our cups</h1> <img src="beker.svg" alt="A cup with three scoops of ice cream"></main>An empty alt for decoration
Not every image tells you something. If the name is already next to it, or it's only decoration, give it an empty alt: alt="". A screen reader then skips the image.
Never just leave alt out. A screen reader then can't tell whether the image matters, and sometimes reads out the file name: "hoorntje dot s v g".
<main> <h1>Choose your ice cream</h1> <p><img src="hoorntje.svg" alt=""> Cone</p> <p><img src="beker.svg" alt=""> Cup</p></main>Try it yourself
Between the heading and the paragraph, add the image wafel.svg, with the description A waffle dusted with icing sugar.
<main> <h1>Waffles</h1> <p>Freshly baked, every afternoon.</p></main>body { margin: 0; font-family: system-ui, sans-serif; background: #fff8f0; color: #3b2a1a;}header { padding: 24px 16px; background: #ffd6e0;}.logo { display: inline-block; margin: 8px 0 0; padding: 6px 18px; border-radius: 999px; background: #ffffff; font-size: 28px;}.slogan { margin: 8px 0 0;}main { padding: 8px 16px 24px;}Ready for more?
The full lesson has 6 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson