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.
<img> tagsrc, alt, width, height, loadingalt text for
accessibility & SEOWhat 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).
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.
alt="K2infocom students coding in a classroom in Vadodara"
alt="image" or alt="photo1.jpg" or alt="" (on a meaningful image)
alt="" (intentionally empty)
alt="HTML CSS JavaScript tutorial free Vadodara Gujarat India"
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:
Code Examples — Images in Practice
Here are the most common ways you'll use the <img> tag:
<!-- 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:
/* Make ALL images responsive by default */
img {
max-width: 100%;
height: auto;
display: block;
}
display: block removes that gap and makes layout more predictable.<!-- 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> 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.
Image as a Link & Lazy Loading
Two more important image techniques every developer should know:
<!-- Clickable image — wrap <img> inside <a> -->
<a href="/courses.html" title="View our courses">
<img
src="images/courses-banner.webp"
alt="Browse web development courses at K2infocom"
width="600"
height="200"
>
</a>
<!-- Lazy loading — defers images below the fold -->
<!-- Use eager for above-fold (hero) images -->
<img
src="images/hero.webp"
alt="K2infocom hero image"
width="1200"
height="600"
loading="eager"
>
<!-- Use lazy for below-fold images -->
<img
src="images/team.webp"
alt="K2infocom teaching team"
width="800"
height="400"
loading="lazy"
>
Common Mistakes — Do's & Don'ts
These are the most frequent image mistakes beginners make:
<!-- 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="">
<!-- 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">
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:
<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.
src — the browser
displays the alt text instead. This shows exactly why alt text matters. Change the alt value
and see the result instantly.
Quick Check — Chapter 7
Expand each question to check your understanding before testing yourself with the multiple choice quiz.
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.
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.
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.
<img> tag?loading defers image loading
until the image is near the viewport?- The
<img>tag is self-closing — no closing tag needed.srcandaltare 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
widthandheightin 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. Useeagerfor 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.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions, and stay updated with new tutorials.