Tutorials HTML HTML Images
Ch 7 / 40
Chapter 7 Beginner 8 min read HTML

HTML Images — The <img> Tag

Images make web pages visual, engaging, and informative. The HTML <img> tag is a self-closing element that embeds images directly into a web page. In this chapter you'll master every attribute, understand when to use each image format, write meaningful alt text, and make your images fully responsive.

What you'll learn
Syntax of the self-closing <img> tag
Key attributes — src, alt, width, height, loading
Writing effective alt text for accessibility & SEO
Image formats — JPG, PNG, SVG, WebP, GIF, AVIF
Making images fully responsive with CSS
Lazy loading, figure & figcaption, image as a link

What is the HTML <img> Tag?

The <img> tag embeds an image into a web page. It is an inline, self-closing element — it has no closing tag and renders within the flow of surrounding text. The two required attributes are src (the image source path) and alt (the alternative text description).

<img src="images/logo.png" alt="K2infocom logo" width="200" height="80" loading="lazy" >
<img> Self-closing — no </img> closing tag needed
src Path or URL of the image file — required
alt Text description for screen readers & SEO — required
width / height Reserves space — prevents layout shift while loading
loading "lazy" defers off-screen images — improves performance

Key Attributes of the <img> Tag

Here is a complete reference of every important attribute the <img> tag supports:

Attribute Required? Purpose Example
src Yes Path or URL of the image. Can be relative (images/photo.jpg) or absolute (https://...) src="logo.png"
alt Yes Descriptive text shown if the image fails to load — read by screen readers and indexed by search engines alt="K2infocom logo"
width Recommended Width in pixels. Tells the browser the image dimensions before it loads — prevents layout shift (CLS) width="400"
height Recommended Height in pixels. Always specify both width and height together to reserve the correct aspect ratio space height="300"
loading Optional lazy defers loading until image enters the viewport. eager loads immediately (default) loading="lazy"
title Optional Tooltip text shown when the user hovers over the image — supplements but does not replace alt title="Company logo"
decoding Optional Hints when the browser should decode the image. async allows rendering to continue without waiting for the image decoding="async"

Writing Effective alt Text

The alt attribute is one of the most important and most misused attributes in HTML. It serves three critical roles: it is read aloud by screen readers for visually impaired users, it displays when an image fails to load, and it is indexed by search engines as part of image SEO.

Good alt text
alt="K2infocom students coding in a classroom in Vadodara"
Descriptive, specific, and tells the user what the image actually shows.
Bad alt text
alt="image" or alt="photo1.jpg" or alt="" (on a meaningful image)
Meaningless to screen readers — gives no context about what the image shows.
Decorative image (empty alt)
alt="" (intentionally empty)
Correct for purely decorative images (backgrounds, dividers). Screen readers skip them.
Keyword stuffing
alt="HTML CSS JavaScript tutorial free Vadodara Gujarat India"
Keyword stuffing harms SEO and makes the experience worse for screen reader users.
Writing Rule
Describe the image as you would explain it to someone over the phone. Be specific and contextual — but keep it under 125 characters. Never start with "Image of..." or "Picture of..." — screen readers already announce it's an image.

Image Formats — Which One to Use?

Choosing the right image format affects your page's load speed, quality, and browser compatibility. Here is a guide to every major format:

.jpg JPEG Good
Best for photographs and images with many colours. Uses lossy compression — smaller file size at the cost of some quality. Does not support transparency.
Photos, hero images, blog thumbnails
.png PNG Good
Supports transparent backgrounds. Lossless compression — perfect quality but larger file size than JPG. Great for logos, icons, and graphics with text.
Logos, icons, screenshots, transparent images
.svg SVG Best for icons
Vector format — scales to any size without losing quality. Tiny file size. Can be styled with CSS and animated. Perfect for logos and UI icons.
Logos, icons, illustrations, UI elements
.webp WebP Best overall
Google's modern format — 25–35% smaller than JPG/PNG at the same quality. Supports transparency and animations. Supported by all modern browsers.
All web images — the recommended default format
.gif GIF Limited
Supports basic animations but is limited to 256 colours. Large file size for animations. Modern alternative is WebP or CSS/JS animations.
Simple animations, memes (use WebP if possible)
.avif AVIF Next-gen
Newest format — even smaller than WebP with better quality. Excellent browser support as of 2024. The future of web images.
High-performance sites targeting modern browsers

Code Examples — Images in Practice

Here are the most common ways you'll use the <img> tag:

HTML — Image Examples
<!-- Basic image with required attributes -->
<img
  src="images/banner.jpg"
  alt="K2infocom web development classroom"
>

<!-- With width, height and lazy loading (recommended) -->
<img
  src="images/profile.webp"
  alt="Instructor at K2infocom Vadodara"
  width="400"
  height="300"
  loading="lazy"
  decoding="async"
>

<!-- Absolute URL (external image) -->
<img
  src="https://k2infocom.com/images/logo.png"
  alt="K2infocom logo"
  width="180"
  height="60"
>

<!-- Decorative image — empty alt tells screen readers to skip it -->
<img
  src="images/decorative-divider.svg"
  alt=""
  width="800"
  height="4"
>

Responsive Images

By default, images render at their exact pixel dimensions and will overflow their container on small screens. The standard way to make any image fully responsive in CSS is one simple rule:

CSS — Making images responsive
/* Make ALL images responsive by default */
img {
  max-width: 100%;
  height: auto;
  display: block;
}
max-width: 100%
The image will never be wider than its parent container. On large screens it shows at full size; on small screens it shrinks proportionally.
height: auto
Automatically maintains the original aspect ratio as the width changes. Prevents the image from being squished or stretched.
display: block
Images are inline by default which adds an unwanted bottom gap. display: block removes that gap and makes layout more predictable.
HTML — figure & figcaption with responsive image
<!-- figure wraps an image with its caption semantically -->
<figure>
  <img
    src="images/html-course.webp"
    alt="Students attending the HTML course at K2infocom Vadodara"
    width="800"
    height="450"
    loading="lazy"
    style="max-width:100%;height:auto;"
  >
  <figcaption>
    Students at K2infocom's HTML & CSS workshop, Vadodara
  </figcaption>
</figure>
figure vs div
Use <figure> with <figcaption> when the image has a visible caption. It is a semantic element — search engines understand that the caption belongs to the image, improving image SEO. Use a <div> only when no caption is needed.

Common Mistakes — Do's & Don'ts

These are the most frequent image mistakes beginners make:

Do — Correct
<!-- Descriptive alt + dimensions -->
<img
  src="logo.webp"
  alt="K2infocom logo"
  width="180"
  height="60"
  loading="lazy"
>

<!-- Decorative — intentional empty alt -->
<img src="divider.svg"
     alt="">
Don't — Wrong
<!-- Missing alt — invalid HTML -->
<img src="logo.png">

<!-- Vague alt — useless to screen readers -->
<img
  src="photo.jpg"
  alt="image"
>

<!-- No dimensions — causes layout shift -->
<img src="banner.jpg"
     alt="Banner">
Always Specify Dimensions
Without width and height, the browser doesn't know the image's size until it downloads. This causes Cumulative Layout Shift (CLS) — content jumping around as the image loads — which hurts both user experience and your Core Web Vitals score in Google Search.

Complete Example — Image Gallery Card

Here is a realistic HTML card combining everything covered in this chapter:

HTML — Course Card with Image
<article class="course-card">

  <!-- Clickable thumbnail that links to the course page -->
  <a href="/courses/web-development.html">
    <img
      src="images/web-dev-course.webp"
      alt="Web Development with AI course thumbnail"
      width="400"
      height="225"
      loading="lazy"
      decoding="async"
      style="max-width:100%;height:auto;display:block;"
    >
  </a>

  <figure>
    <img
      src="images/instructor.webp"
      alt="Lead instructor smiling at K2infocom"
      width="48"
      height="48"
    >
    <figcaption>Taught by K2infocom Certified Instructors</figcaption>
  </figure>

  <h3>Modern Web Development With AI</h3>
  <p>120+ chapters · 40 hours of content · Beginner to Advanced</p>

</article>

Try It Yourself

Edit the image code below and click Open in Editor to see your changes live in full screen.

Live Editor
✎ Editor
👁 Preview
Try This
Notice the third image intentionally has a broken src — the browser displays the alt text instead. This shows exactly why alt text matters. Change the alt value and see the result instantly.
Free Resource
HTML5 Cheat Sheet — All Tags & Attributes
One printable PDF covering the img tag, all its attributes, image formats, responsive techniques, and accessibility rules.
Download PDF
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React, and Node.js in one industry-ready curriculum with real projects and placement support.
120+
Chapters
40 hrs
Content
10
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js MongoDB AI Tools

Quick Check — Chapter 7

Expand each question to check your understanding before testing yourself with the multiple choice quiz.

Questions & Answers
The alt attribute is required for three critical reasons. First, accessibility — screen readers read the alt text aloud to visually impaired users so they understand what the image shows. Second, fallback — if the image fails to load (broken URL, slow connection), the browser displays the alt text in its place. Third, SEO — search engines cannot "see" images, so they rely on the alt attribute to understand what the image contains and index it appropriately. An image without alt is technically invalid HTML and fails accessibility standards.
When a browser loads a page, it lays out the content before images have downloaded. Without width and height, it doesn't know how much space to reserve for each image. Once the image downloads and renders, it pushes surrounding content out of the way — this is called Cumulative Layout Shift (CLS). It causes text and buttons to jump around, which is a poor user experience and also hurts your Core Web Vitals score in Google Search. Specifying dimensions lets the browser reserve the exact space before downloading.
loading="lazy" tells the browser to defer loading an image until it is close to entering the user's viewport. Instead of downloading all images at once when the page loads, the browser only loads images the user is about to see. This significantly reduces initial page load time and data usage — especially on mobile. Use loading="lazy" on all images below the fold (not visible on first page load). Use loading="eager" (the default) for above-fold images like hero images or logos, since those should load immediately.
For photographs, the best format is WebP — it is 25–35% smaller than JPG at equal quality and is supported by all modern browsers. If you need IE11 support, fall back to JPG. For logos, icons, and simple graphics, use SVG — it is a vector format that scales to any size without losing quality, and the file size is tiny. If the logo contains complex gradients or photos, use WebP. For graphics that require transparency (like PNG), also prefer WebP. AVIF is the next-generation choice for maximum compression.
The standard CSS technique is to apply max-width: 100% and height: auto to all images. max-width: 100% ensures the image never exceeds the width of its container — on large screens it shows at full size, on small screens it shrinks. height: auto maintains the original aspect ratio as the width changes. Adding display: block removes the default inline gap at the bottom of images. You still set the actual width and height attributes in HTML for layout shift prevention — the CSS then overrides them to be responsive.
<figure> is a semantic HTML5 element specifically designed to group self-contained content — typically an image, diagram, or code snippet — with an optional <figcaption>. Search engines understand the association between the image and its caption when wrapped in <figure>, improving image SEO. Screen readers also understand the relationship. A <div> is a generic, non-semantic container — it has no meaning to browsers, search engines, or assistive technology. Use <figure> + <figcaption> when the image has a visible caption; use <div> for layout purposes only.
Multiple Choice Questions
MCQ 1 of 4
Which two attributes are required on every HTML <img> tag?
MCQ 2 of 4
Which CSS property makes an image never wider than its parent container?
MCQ 3 of 4
Which image format is recommended for photographs on the modern web due to its small file size?
MCQ 4 of 4
What value of loading defers image loading until the image is near the viewport?
Chapter Summary
  • The <img> tag is self-closing — no closing tag needed. src and alt are both required attributes.
  • Always write descriptive alt text — it is read by screen readers, shown on broken images, and indexed by search engines. Empty alt="" is correct only for purely decorative images.
  • Always specify width and height in HTML to prevent Cumulative Layout Shift (CLS) — a Core Web Vitals metric.
  • Use loading="lazy" on below-fold images to defer loading and improve page performance. Use eager for hero images.
  • Make images responsive with CSS: max-width: 100%; height: auto; display: block;
  • WebP is the recommended format for photos. SVG for logos and icons. AVIF for maximum next-gen compression.
  • Use <figure> + <figcaption> for images with captions — it is semantic and improves SEO. Wrap in <a> to make an image clickable.
~8 min read
Beginner level
Chapter 7 of 40

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
If this tutorial helped you, a 5-star Google review means a lot. It helps other learners discover our free content and motivates us to keep improving.
500+ Reviews (Target)
Write a Google Review
Join WhatsApp Channel