Lesson 4 of 29 · HTML Basics
HTML Images: The img Tag, src and alt Text
A page without pictures is a wall of text. The <img> element puts images on the page — photos, logos, icons — and with one small attribute, alt, it makes sure everyone gets the message, including people who can't see the picture.
The <img> element
<img src="/icons/owl.svg" alt="A wise owl">
src(source) is the address of the image file. It works like a link'shref: an absolute URL for images on other sites, a relative path for your own files.alt(alternative text) describes the image in words.
Like <br>, <img> is an empty element: there is no closing tag. The browser fetches the file and shows the picture right where the tag is, in the flow of the text.
Why alt text matters
The alt text isn't a nice extra — it's what the image means for everyone who can't see it:
- Screen readers read it aloud to blind and visually impaired visitors.
- If the image fails to load (a typo in the path, a slow connection), the browser shows the alt text instead.
- Search engines can't look at pictures. They read the alt text to understand an image and to show it in image search.
Good alt text is short and describes what matters in context: "A golden crown", not "crown.svg" or "picture of a crown" — screen readers already announce that it's an image. If an image is pure decoration, keep the attribute but leave it empty, alt="", so screen readers skip it. Leaving alt out completely is the one wrong choice: many screen readers then read out the file name instead.
Setting the size
The width and height attributes set the size in pixels. Write just the number, without px. If you give only one of them, the browser scales the other to keep the image's proportions:
<img src="/icons/dragon.svg" alt="A green dragon" width="120">
Giving the size up front also lets the browser reserve the space before the file arrives, so the text doesn't jump around while the page loads. Later in the course, CSS gives you even more control over sizes.
Which file format?
Use JPEG or WebP for photos, PNG for screenshots and images with sharp edges, and SVG for icons and logos. SVG images are drawn from shapes rather than pixels, so they stay crisp at any size — all the icons in this course are SVGs.
Common mistakes
- No
altattribute. Every image needs one, even if it's empty for decoration. width="96px". The attribute takes a plain number:width="96".- A wrong path. One typo and you get a broken-image icon. Paths are case-sensitive on most servers:
Owl.svgis notowl.svg. - A closing tag.
<img …></img>is invalid;<img>stands alone.
Assignment
The trophy shelf has an owl that screen readers can't describe. Fix it, then add two trophies:
- Give the existing owl image the alt text
A wise owl. - After the owl, add a crown:
src="/icons/crown.svg", alt textA golden crown,width="96". - Then add a gem:
src="/icons/gem.svg", alt textA blue gem,width="96".
The images appear side by side in the Preview — like words in a sentence, they flow along the line.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .