A page without images feels bare — but images done badly are also the number-one cause of slow, broken-looking websites. The <img> element is simple, and getting two attributes right (src and alt) puts you ahead of a surprising number of real sites.
1The img element
<img> is a void element — no closing tag. Instead of content, you point it at a file with src and describe it with alt:
<img src="logo.png" alt="Vidaara logo">
src— the image file. It can be a relative path (images/logo.png), an absolute URL, or a data URI.alt— a short text description. This is not optional in practice: screen readers read it aloud, search engines index it, and it shows if the image fails to load.
alt="" so screen readers skip them.<img>is a void element — no closing tag.srcpoints to the image file;altdescribes it.- Always write meaningful
altfor content images; usealt=""for purely decorative ones.
2Fast, responsive images
Two habits keep images from hurting your page:
Don't let images overflow
A big image can blow out your layout on a phone. The standard fix (you'll meet it again in the CSS track) is one CSS rule: img { max-width: 100%; height: auto; }. This lets an image shrink to fit its container while keeping its proportions.
Help the browser and save data
- Add
widthandheightattributes so the browser reserves space and the page doesn't jump around as images load. - Add
loading="lazy"so off-screen images only download when the user scrolls near them — a real saving on slow connections. - Choose a sensibly sized file. A 4000-pixel photo for a small thumbnail wastes your visitor's data, especially on mobile.
<img src="team.jpg" alt="Our volunteers"
width="800" height="500" loading="lazy">
img { max-width: 100%; height: auto; }stops images overflowing on small screens.- Set
widthandheightso the page doesn't jump as images load. loading="lazy"defers off-screen images, saving data on slow connections.
★ Practical: a captioned, accessible image
In any playground, add an image with:
- An
<img>with a meaningfulaltdescribing what it shows. - A
<p>caption underneath it. - Add
loading="lazy"to the image. - Empty the
srcvalue once to confirm the alt text appears in its place.
Ready to test yourself?
Take the short quiz. Score 60% or more to mark this lesson complete.
Start the quiz →💡 Log in to save your progress and earn the certificate.