A picture of a perfectly poured latte sells more coffee than a paragraph about it. Images are added with the <img> element, a void element with no closing tag.
<img src="images/latte.jpg" alt="A latte topped with milk foam shaped like a leaf">Two attributes are required: src (where the image file lives) and alt (a text alternative).
The src attribute#
src works like href. Use a relative path for images in your project, or an absolute URL for images hosted elsewhere. Keep images in an images folder so your project stays tidy, and use lowercase file names without spaces.
The alt attribute#
alt describes the image in words. It is displayed if the image fails to load, and it is read aloud by screen readers. It also helps search engines.
How to write good alt text:
- Describe the purpose, not just the pixels. "Barista pouring milk into a cup" beats "image1".
- Keep it short, usually under a sentence.
- Do not start with "image of" or "picture of". Screen readers already announce an image.
- If the image is purely decorative, use an empty attribute:
alt="". This tells assistive technology to skip it. Do not leavealtout entirely.
<!-- informative -->
<img src="images/team.jpg" alt="The three baristas laughing behind the counter">
<!-- decorative divider: intentionally empty alt -->
<img src="images/swirl.png" alt="">Width and height#
Add width and height attributes with the image's real dimensions in pixels:
<img src="images/latte.jpg" alt="A latte with leaf-shaped foam" width="800" height="600">The browser then reserves space before the file downloads, preventing the page from jumping around as images arrive. With CSS you can still make the image scale down (for example max-width: 100%; height: auto;) while keeping the correct aspect ratio.
Do not use HTML sizes to shrink a giant photo. A 4000-pixel wide photo displayed at 400 pixels is still a heavy download. Resize the file itself.
Lazy loading#
<img src="images/gallery-12.jpg" alt="Our sunny terrace" width="800" height="533" loading="lazy">loading="lazy" tells the browser to postpone off-screen images until the visitor scrolls near them. Do not use it on the main image at the top of the page, because you want that one immediately.
Choosing a format#
| Format | Best for |
|---|---|
| JPEG | Photographs |
| PNG | Screenshots, graphics needing transparency |
| WebP and AVIF | Modern, smaller alternatives for both |
| SVG | Logos and icons that must stay sharp (see the media section) |
Images as links#
<a href="menu.html">
<img src="images/menu-card.jpg" alt="View the full menu" width="300" height="200">
</a>When an image is the only content of a link, its alt text becomes the link text, so it must describe the destination or action.
Common mistakes#
- Omitting
alt, which is both an accessibility failure and invalid HTML. - Writing the file name as alt text ("IMG_2043.jpg").
- Wrong path or capitalisation, producing a broken image icon. Servers are often case sensitive even if your laptop is not.
- Uploading huge originals.
- Lazy-loading the top banner.
Try it yourself#
Place two images in an images folder. Add them to a page with correct src, thoughtful alt, and width and height. Make the second image decorative using an empty alt. Then wrap the first image in a link to another page and rewrite its alt to describe the destination. Break a path on purpose to see the broken image behaviour and the alt text appear.
Recap#
Use img with src and meaningful alt. Give images width and height to avoid layout jumps, resize files before uploading, and lazy-load those below the fold. Decorative images get an empty alt, never a missing one.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -