Working with Images in HTML: A Complete Guide to the <img> Tag
Master HTML images with this comprehensive guide covering the img tag, alt text, file formats, responsive images, srcset, and web performance optimization.
Master HTML images with this comprehensive guide covering the img tag, alt text, file formats, responsive images, srcset, and web performance optimization.
<img>
TagImages are essential elements of modern web design, making websites more engaging, informative, and visually appealing. In this comprehensive guide, you'll learn everything you need to know about working with images in HTML, from basic implementation to advanced optimization techniques.
<img>
Tag ExplainedThe <img>
tag is a self-closing HTML element used to embed images in web pages. Unlike other HTML elements, it doesn't require a closing tag and uses attributes to define the image source and properties.
<img src="path/to/image.jpg" alt="Description of the image" />
<img>
AttributesRequired Attributes:
src
: Specifies the path to the image filealt
: Provides alternative text for accessibilityOptional Attributes:
width
: Sets the image width in pixelsheight
: Sets the image height in pixelstitle
: Adds a tooltip when hovering over the imageloading
: Controls when the image loads (lazy loading)<img
src="images/sunset-beach.jpg"
alt="Beautiful sunset over a tropical beach with palm trees"
width="800"
height="600"
title="Sunset Beach Paradise"
loading="lazy"
/>
Alt text (alternative text) is crucial for web accessibility and SEO. It serves multiple purposes:
Good Alt Text Examples:
<!-- Descriptive and specific -->
<img
src="golden-retriever.jpg"
alt="Golden retriever playing fetch in a sunny park"
/>
<!-- Functional description for buttons -->
<img src="search-icon.png" alt="Search" />
<!-- Empty alt for decorative images -->
<img src="decorative-border.png" alt="" />
Alt Text Best Practices:
alt=""
) for purely decorative imagesPoor Alt Text Examples:
<!-- Too vague -->
<img src="dog.jpg" alt="dog" />
<!-- Redundant -->
<img src="sunset.jpg" alt="Image of a sunset" />
<!-- Too long -->
<img
src="office.jpg"
alt="A very large modern office building with glass windows and steel frame construction located in downtown area with people walking by"
/>
Choosing the right image format is crucial for web performance and quality. Here are the main formats and their use cases:
Best for: Photographs and images with many colors
Characteristics:
<img src="landscape-photo.jpg" alt="Mountain landscape at sunrise" />
Best for: Images with transparency, logos, graphics with few colors
Characteristics:
<img src="company-logo.png" alt="Mediaweb company logo" />
Best for: Modern web applications (when supported)
Characteristics:
<picture>
<source srcset="hero-image.webp" type="image/webp" />
<img src="hero-image.jpg" alt="Hero banner showing our product" />
</picture>
Best for: Simple graphics, icons, logos that need to scale
Characteristics:
<img src="arrow-icon.svg" alt="Right arrow" />
Best for: Simple animations (use sparingly)
Characteristics:
Modern websites must work on various screen sizes and resolutions. Responsive images ensure optimal performance and quality across all devices.
srcset
AttributeThe srcset
attribute allows you to provide multiple image sources for different screen conditions:
<img
src="image-800w.jpg"
srcset="image-400w.jpg 400w, image-800w.jpg 800w, image-1200w.jpg 1200w"
sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"
alt="Responsive landscape image"
/>
Width Descriptors (w
):
srcset=" small-image.jpg 400w, medium-image.jpg 800w, large-image.jpg 1200w "
Density Descriptors (x
):
srcset=" image.jpg 1x, image-2x.jpg 2x, image-3x.jpg 3x "
<picture>
ElementFor more complex responsive image scenarios, use the <picture>
element:
<picture>
<!-- Mobile: smaller, cropped image -->
<source media="(max-width: 600px)" srcset="mobile-hero.jpg" />
<!-- Tablet: medium-sized image -->
<source media="(max-width: 1024px)" srcset="tablet-hero.jpg" />
<!-- Desktop: full-sized image -->
<source media="(min-width: 1025px)" srcset="desktop-hero.jpg" />
<!-- Fallback for older browsers -->
<img src="desktop-hero.jpg" alt="Hero banner showcasing our services" />
</picture>
Use different images for different screen sizes:
<picture>
<!-- Mobile: portrait orientation -->
<source media="(max-width: 600px)" srcset="portrait-image.jpg" />
<!-- Desktop: landscape orientation -->
<img src="landscape-image.jpg" alt="Team collaboration in modern office" />
</picture>
Image optimization is crucial for fast-loading websites. Here are essential techniques:
<!-- Don't do this: large image scaled down -->
<img
src="huge-image-4000x3000.jpg"
width="400"
height="300"
alt="Product photo"
/>
<!-- Do this: appropriately sized image -->
<img src="product-400x300.jpg" width="400" height="300" alt="Product photo" />
<!-- Modern browsers -->
<img src="image.jpg" alt="Description" loading="lazy" />
<!-- With intersection observer fallback -->
<img
src="placeholder.jpg"
data-src="actual-image.jpg"
alt="Description"
class="lazy-load"
loading="lazy"
/>
For JPEG images:
For PNG images:
<picture>
<!-- Modern format for supported browsers -->
<source srcset="image.avif" type="image/avif" />
<source srcset="image.webp" type="image/webp" />
<!-- Fallback for all browsers -->
<img src="image.jpg" alt="Description" />
</picture>
<!-- Prevents layout shift -->
<img
src="image.jpg"
alt="Description"
width="800"
height="600"
style="max-width: 100%; height: auto;"
/>
Create clickable areas within an image:
<img src="world-map.jpg" alt="World map" usemap="#worldmap" />
<map name="worldmap">
<area
shape="rect"
coords="0,0,100,100"
href="north-america.html"
alt="North America"
/>
<area shape="circle" coords="200,200,50" href="europe.html" alt="Europe" />
</map>
Provide semantic meaning and captions:
<figure>
<img src="chart.jpg" alt="Sales growth chart showing 25% increase" />
<figcaption>
Sales Growth: Q4 2024 shows 25% increase over previous quarter
</figcaption>
</figure>
Style images with CSS:
<img src="profile.jpg" alt="User profile photo" class="profile-image" />
.profile-image {
width: 150px;
height: 150px;
border-radius: 50%;
object-fit: cover;
border: 3px solid #007bff;
}
<!-- Wrong -->
<img src="important-chart.jpg" />
<!-- Correct -->
<img
src="important-chart.jpg"
alt="Revenue growth chart showing 40% increase"
/>
<!-- Wrong: text in image -->
<img src="heading-text.jpg" alt="Welcome to Our Website" />
<!-- Correct: actual text -->
<h1>Welcome to Our Website</h1>
<!-- Wrong: huge file -->
<img src="photo-5mb.jpg" alt="Team photo" />
<!-- Correct: optimized file -->
<img src="photo-optimized-200kb.jpg" alt="Team photo" />
<!-- Wrong: fixed size -->
<img src="hero.jpg" width="1200" height="800" alt="Hero image" />
<!-- Correct: responsive -->
<img
src="hero.jpg"
srcset="hero-400w.jpg 400w, hero-800w.jpg 800w, hero-1200w.jpg 1200w"
sizes="100vw"
alt="Hero image"
style="max-width: 100%; height: auto;"
/>
Working with images in HTML involves much more than just adding an <img>
tag to your page. By understanding proper implementation, accessibility requirements, file formats, responsive techniques, and optimization strategies, you can create websites that are fast, accessible, and visually appealing across all devices.
Remember that images significantly impact both user experience and website performance. Take the time to implement these techniques properly, and your users will thank you with better engagement and faster page loads.
Start implementing these image optimization techniques in your next project, and you'll see immediate improvements in both performance and accessibility scores.
Continue reading with these related articles
Master HTML lists with this complete guide covering ul, ol, and dl elements, nesting techniques, styling with CSS, and accessibility best practices.
Learn HTML tables from basics to advanced techniques. Master table, tr, td, th elements, accessibility, responsive design, and when to use tables properly.
Learn how to create interactive HTML forms with input types, labels, buttons, and basic validation. Master form accessibility and best practices.
Discover the power of semantic HTML elements like header, footer, article, and section. Learn how semantic markup improves SEO, accessibility, and code maintainability.