HTML Images

Images in HTML: Telling Visual Stories on the Web

Images make content more engaging, informative, and emotional. Whether you're showcasing a fruit, illustrating a concept, or enhancing user experience — HTML gives you powerful tools to embed and control images effortlessly.

The <img> Tag: Displaying an Image

The <img> tag is how you add images to a webpage. It’s self-closing and requires at least one attribute: src, which defines the path to your image.

<img src="apple.jpg" alt="Red apple">

Key attributes:

  • src: The image file location
  • alt: Descriptive alternative text shown if the image fails or for screen readers

Using Alt Text: Accessibility & Fallback

Alt text is not an afterthought — it’s essential. It improves accessibility and SEO. Let’s see what happens when the image can’t be found:

<img src="banana.jpg" alt="A ripe banana">
[Image of a ripe banana or "A ripe banana" if the file is missing]

The alt text acts like a verbal description — use it meaningfully.

Image Sizing and Alignment

You can control how big your image appears using the width and height attributes.

<img src="cherry.jpg" alt="Fresh cherries" width="200" height="150">
[Image of cherries scaled to 200x150 pixels]

Alternatively, CSS (later lessons) offers more flexible sizing. Avoid distorting the aspect ratio unless intentional.

Responsive Images: Fit All Screens

Today’s websites must look good on phones, tablets, and desktops. Make images responsive by using percentages or CSS, but HTML alone can help too.

<img src="fruit-basket.jpg" alt="Basket of fruits" style="max-width: 100%; height: auto;">
[Image scales with the width of the container, preserving proportions]

This makes the image fluid — resizing based on the screen size. It's essential for mobile-friendly design.

Figure and Figcaption: Meaningful Visual Context

Sometimes, an image needs a caption. HTML5 introduced the <figure> and <figcaption> tags for this purpose.

<figure>
  <img src="apple.jpg" alt="Red apple on a white background" width="200">
  <figcaption>A fresh apple ready to eat.</figcaption>
</figure>
[Image of an apple]
A fresh apple ready to eat.

The <figcaption> gives the image a voice. It enhances understanding, especially in articles or educational content.

Examples Recap

Basic Image:

<img src="apple.jpg" alt="Red apple">

Missing Image with Alt Text:

<img src="missing.jpg" alt="This image is unavailable">

Responsive Image with Style:

<img src="fruit.jpg" alt="Fruit display" style="max-width: 100%; height: auto;">

Figure with Caption:

<figure>
  <img src="banana.jpg" alt="Yellow banana">
  <figcaption>A banana placed on a table.</figcaption>
</figure>

Best Practices for Images in HTML

  • Always use descriptive alt text — it helps users and search engines
  • Keep images optimized — large files slow down page loading
  • Use relative paths for local files, and full URLs for external images
  • Use figure and figcaption when your image adds visual context to content

Summary

You’ve just unlocked the visual dimension of web content. Now you know how to:

  • Use the <img> tag to display images
  • Write effective and meaningful alt text
  • Resize and align images with attributes and inline styles
  • Create responsive images that adapt to screens
  • Enhance semantic structure with <figure> and <figcaption>

Check your understanding 1

Which tag is used in HTML to embed an image?
Choose one answer. You can try again.

Answer and explanation

<code><img></code>

The correct tag to insert an image in HTML is <img>. It is self-closing and requires at least a src and alt attribute.

Check your understanding 2

The alt attribute provides a text alternative for screen readers and displays if the image fails to load.
Choose one answer. You can try again.

Answer and explanation

True

Yes, the alt attribute improves accessibility and acts as fallback content when the image URL is broken or the image cannot be displayed.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. width
  2. height
  3. alt
  4. float
Which of the following attributes can be used with the <img> tag to control image appearance?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 3

You can use width, height, and alt directly on the <img> tag. To apply float, you’d use CSS either inline or via stylesheets.

Check your understanding 4

Which HTML structure is ideal when you want to include a caption with an image?
Choose one answer. You can try again.

Answer and explanation

&lt;figure&gt;&lt;img src="event.jpg"&gt;&lt;figcaption&gt;Annual Day Event&lt;/figcaption&gt;&lt;/figure&gt;

The combination of <figure> and <figcaption> is semantically correct and accessible. It associates an image with a caption, making it clearer for readers and screen readers alike.

Check your understanding 5

Setting an image width to 100% makes it responsive to its container.
Choose one answer. You can try again.

Answer and explanation

True

Yes, using width: 100% in CSS or directly in the <img> tag makes the image scale relative to its parent container, ensuring responsiveness across devices.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. <img src="classroom.jpg" width="100%" alt="10th class students in classroom">
  2. <img alt="No image source">
  3. <img src="sports-day.png" style="max-width: 100%; height: auto;" alt="Sports Day celebration">
  4. <img src="sciencefair.jpg" float="right">
Which examples are valid and correctly formatted responsive or accessible images?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 3

The first and third examples are responsive and include good alt attributes. The second lacks src and the fourth incorrectly uses float as an HTML attribute instead of CSS.

What’s Next?

Now that your content has visual life, let’s move into structured layouts. In the next lesson, we’ll build and format Tables in HTML — for displaying data cleanly and clearly.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Tables →