On this page11 sections
  1. Learning Objectives
  2. 1. The Image Element
  3. 2. Writing Good alt Text
  4. 3. Width and Height
  5. 4. Image Formats
  6. 5. Captions With <figure>
  7. 6. Lazy Loading
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 011: Images

Category3: Links, Images, Tables & Lists
Lecture011 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

A page without pictures feels empty. The <img> element puts a photo, logo or diagram on the page. It takes one line, but the alt text and the image size need a little care.


Learning Objectives

  • Add images with <img src="..." alt="...">
  • Write useful alt text
  • 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

html
<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 like images/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.

ImageGood alt
Product photoalt="Red cotton kurta, front view"
A logo that links homealt="Coding Mentor home"
A purely decorative swirlalt="" (empty, so screen readers skip it)

Example: the alt text shows when the image fails to load:

html
<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

html
<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

FormatBest for
JPGPhotos
PNGGraphics needing a transparent background
SVGLogos and icons. Stays sharp at any size
WebPPhotos and graphics, smaller files than JPG/PNG

Smaller files make pages load faster, especially on mobile data.


5. Captions With <figure>

Example

html
<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.

html
<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

  1. Which two attributes should every <img> have?
  2. Does <img> need a closing tag?
  3. Write good alt text for a photo of a blue school bag.
  4. Why set width and height on images?
  5. Which format is best for a logo that must stay sharp at every size?
  6. Write HTML for an image with the caption "Our team, 2026".

Practice Answers

  1. src and alt.
  2. No. It is an empty element.
  3. alt="Blue school bag with two front pockets"
  4. So the browser saves space for the image and the page doesn't jump while loading.
  5. SVG.
  6. html
    <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.
  • alt describes the image; leave it empty only for pure decoration.
  • Set width and height to stop layout jumps; keep files small.
  • <figure> + <figcaption> add a caption; loading="lazy" speeds up long pages.