On this page11 sections
Lecture 011: Images
| Category | 3: Links, Images, Tables & Lists |
| Lecture | 011 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
A page without pictures feels empty. The
<img>element puts a photo, logo or diagram on the page. It takes one line, but thealttext and the image size need a little care.
Learning Objectives
- Add images with
<img src="..." alt="..."> - Write useful
alttext - Set an image's size and stop the page from jumping while it loads
- Add a caption with
<figure>and<figcaption>
1. The Image Element
Example
<img src="https://picsum.photos/id/1015/400/250" alt="A river running through a green valley">src: where the image file is (a full URL, or a relative path likeimages/river.jpg)alt: a short description of the image<img>is an empty element, so it has no closing tag
2. Writing Good alt Text
Describe what matters about the picture, as if telling someone on the phone.
| Image | Good alt |
|---|---|
| Product photo | alt="Red cotton kurta, front view" |
| A logo that links home | alt="Coding Mentor home" |
| A purely decorative swirl | alt="" (empty, so screen readers skip it) |
Example: the alt text shows when the image fails to load:
<img src="missing-photo.jpg" alt="Our shop front on Mall Road">3. Width and Height
Give images a width and height in pixels. The browser then saves the right space before
the image arrives, so the text doesn't jump around as the page loads.
Example
<img src="https://picsum.photos/id/1025/300/200" alt="A pug wrapped in a blanket" width="300" height="200">
<p>This text doesn't move when the image loads.</p>Keep the same shape (ratio) as the real image, or the picture will look stretched.
4. Image Formats
| Format | Best for |
|---|---|
| JPG | Photos |
| PNG | Graphics needing a transparent background |
| SVG | Logos and icons. Stays sharp at any size |
| WebP | Photos and graphics, smaller files than JPG/PNG |
Smaller files make pages load faster, especially on mobile data.
5. Captions With <figure>
Example
<figure>
<img src="https://picsum.photos/id/1043/320/200" alt="Sunlight through autumn trees" width="320" height="200">
<figcaption>Autumn in the northern valleys.</figcaption>
</figure><figure> groups the image with its caption, so both are understood as one unit.
6. Lazy Loading
loading="lazy" tells the browser to download an image only when the visitor scrolls near
it. It's great for long pages with many pictures.
<img src="https://picsum.photos/id/1039/320/200" alt="A waterfall" width="320" height="200" loading="lazy">Common Beginner Mistakes
1. Missing alt. Always include it, even if it's empty for decoration.
2. alt="image" or alt="photo". That tells nobody anything. Describe the content.
3. Wrong path. src="Images/Logo.PNG" and images/logo.png are different files on most
web servers. Match the exact spelling and capitals.
4. Huge files. A 5 MB photo shown 300 pixels wide wastes data. Resize images before uploading.
Practice Questions
- Which two attributes should every
<img>have? - Does
<img>need a closing tag? - Write good
alttext for a photo of a blue school bag. - Why set
widthandheighton images? - Which format is best for a logo that must stay sharp at every size?
- Write HTML for an image with the caption "Our team, 2026".
Practice Answers
srcandalt.- No. It is an empty element.
alt="Blue school bag with two front pockets"- So the browser saves space for the image and the page doesn't jump while loading.
- SVG.
-
<figure> <img src="team.jpg" alt="Five team members smiling in the office" width="400" height="260"> <figcaption>Our team, 2026</figcaption> </figure>
Key Takeaways
<img src="..." alt="...">shows an image; it has no closing tag.altdescribes the image; leave it empty only for pure decoration.- Set
widthandheightto stop layout jumps; keep files small. <figure>+<figcaption>add a caption;loading="lazy"speeds up long pages.