Skip to main content
Mediaweb Logo

Mediaweb

Html Guide

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.

September 4, 2025
7 min read
HTML
images
img tag
alt text
responsive images
web performance

Working with Images in HTML: A Complete Guide to the <img> Tag

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

The <img> Tag Explained

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

Basic Image Syntax

<img src="path/to/image.jpg" alt="Description of the image" />

Essential <img> Attributes

Required Attributes:

  • src: Specifies the path to the image file
  • alt: Provides alternative text for accessibility

Optional Attributes:

  • width: Sets the image width in pixels
  • height: Sets the image height in pixels
  • title: Adds a tooltip when hovering over the image
  • loading: Controls when the image loads (lazy loading)

Complete Example

<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"
/>

The Importance of Alt Text

Alt text (alternative text) is crucial for web accessibility and SEO. It serves multiple purposes:

Why Alt Text Matters

  1. Accessibility: Screen readers use alt text to describe images to visually impaired users
  2. SEO: Search engines use alt text to understand image content
  3. Fallback: Displays when images fail to load
  4. Context: Provides meaning and context for the image

Writing Effective Alt Text

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:

  • Be descriptive but concise (under 125 characters)
  • Include important details and context
  • Avoid "image of" or "picture of"
  • Use empty alt (alt="") for purely decorative images
  • Include text that appears in the image

Poor 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"
/>

Image File Formats and When to Use Them

Choosing the right image format is crucial for web performance and quality. Here are the main formats and their use cases:

JPEG (.jpg, .jpeg)

Best for: Photographs and images with many colors

Characteristics:

  • Lossy compression
  • Small file sizes
  • Supports millions of colors
  • No transparency support
<img src="landscape-photo.jpg" alt="Mountain landscape at sunrise" />

PNG (.png)

Best for: Images with transparency, logos, graphics with few colors

Characteristics:

  • Lossless compression
  • Supports transparency
  • Larger file sizes than JPEG
  • Perfect for logos and icons
<img src="company-logo.png" alt="Mediaweb company logo" />

WebP (.webp)

Best for: Modern web applications (when supported)

Characteristics:

  • Superior compression to JPEG and PNG
  • Supports transparency and animation
  • Not supported by all browsers
  • 25-35% smaller file sizes
<picture>
  <source srcset="hero-image.webp" type="image/webp" />
  <img src="hero-image.jpg" alt="Hero banner showing our product" />
</picture>

SVG (.svg)

Best for: Simple graphics, icons, logos that need to scale

Characteristics:

  • Vector format (scalable)
  • Very small file sizes
  • Perfect sharpness at any size
  • Can be styled with CSS
<img src="arrow-icon.svg" alt="Right arrow" />

GIF (.gif)

Best for: Simple animations (use sparingly)

Characteristics:

  • Supports animation
  • Limited to 256 colors
  • Large file sizes for animations
  • Consider video formats for complex animations

Responsive Images and Srcset

Modern websites must work on various screen sizes and resolutions. Responsive images ensure optimal performance and quality across all devices.

The srcset Attribute

The 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"
/>

Understanding Srcset Syntax

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 "

The <picture> Element

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

Art Direction with 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>

Optimizing Images for Web Performance

Image optimization is crucial for fast-loading websites. Here are essential techniques:

1. Choose the Right Dimensions

<!-- 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" />

2. Implement Lazy Loading

<!-- 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"
/>

3. Use Appropriate Compression

For JPEG images:

  • Use 80-85% quality for most photos
  • Use 90-95% quality for important hero images
  • Use 60-75% quality for thumbnails

For PNG images:

  • Use tools like TinyPNG or ImageOptim
  • Consider converting to WebP when possible

4. Implement Progressive Enhancement

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

5. Add Proper Sizing Attributes

<!-- Prevents layout shift -->
<img
  src="image.jpg"
  alt="Description"
  width="800"
  height="600"
  style="max-width: 100%; height: auto;"
/>

Advanced Image Techniques

Image Maps

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>

Figure and Figcaption

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>

CSS Integration

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;
}

Common Image Mistakes to Avoid

1. Missing Alt Text

<!-- Wrong -->
<img src="important-chart.jpg" />

<!-- Correct -->
<img
  src="important-chart.jpg"
  alt="Revenue growth chart showing 40% increase"
/>

2. Using Images for Text

<!-- Wrong: text in image -->
<img src="heading-text.jpg" alt="Welcome to Our Website" />

<!-- Correct: actual text -->
<h1>Welcome to Our Website</h1>

3. Not Optimizing File Sizes

<!-- Wrong: huge file -->
<img src="photo-5mb.jpg" alt="Team photo" />

<!-- Correct: optimized file -->
<img src="photo-optimized-200kb.jpg" alt="Team photo" />

4. Ignoring Responsive Design

<!-- 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;"
/>

Best Practices Summary

  1. Always include alt text for accessibility and SEO
  2. Choose the right file format based on image type and use case
  3. Optimize file sizes without sacrificing necessary quality
  4. Use responsive images with srcset and sizes attributes
  5. Implement lazy loading for better performance
  6. Include width and height attributes to prevent layout shift
  7. Use semantic HTML with figure and figcaption when appropriate
  8. Test across devices to ensure images display correctly
  9. Consider modern formats like WebP and AVIF with fallbacks
  10. Monitor performance and optimize based on real user data

Conclusion

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.

Last updated: September 5, 2025

Related Articles

Continue reading with these related articles