HTML Basics

HTML Images

Master HTML images for rich, accessible web content. Learn image formats, responsive design, accessibility, and performance optimization.

How do you add an image in HTML?

Use the <img> element with src (the file) and alt (a text description): <img src="cat.jpg" alt="A cat asleep on a laptop" width="800" height="600">. Setting width and height lets the browser reserve space so the page doesn't jump while the image loads, and loading="lazy" delays off-screen images.

<img
  src="/images/hero.webp"
  alt="Developer coding on a laptop at night"
  width="1200" height="675"
  loading="lazy"
  decoding="async">

The Power of HTML Images

Images are crucial for engaging web content, conveying information, and enhancing user experience. Proper HTML image implementation ensures accessibility, performance, and cross-device compatibility.

Image Benefits:

  • Visual Appeal: Enhance content presentation
  • Information: Convey complex data visually
  • Accessibility: Support screen readers with alt text
  • SEO: Improve search engine optimization

Image Element Syntax

<img src="image.jpg" alt="Description" width="300" height="200">
↑ Element ↑ Source ↑ Alt Text ↑ Dimensions

Required Attributes

src=""

Path to the image file

alt=""

Alternative text for accessibility

Optional Attributes

width="" height=""

Dimensions in pixels

loading="lazy"

Defer loading until needed

Image Formats & Best Practices

JPEG (.jpg)

Best for: Photographs, complex images

Pros: Small file size, good compression

Cons: Lossy compression, no transparency

Use when: File size is priority

PNG (.png)

Best for: Graphics, logos, icons

Pros: Lossless, transparency support

Cons: Larger file sizes

Use when: Quality and transparency needed

WebP (.webp)

Best for: Modern web applications

Pros: Excellent compression, transparency

Cons: Limited browser support

Use when: Optimizing for modern browsers

SVG (.svg)

Best for: Vector graphics, icons, simple illustrations

Pros: Scalable, small file size, CSS styleable

Cons: Not suitable for photographs

Use when: Scalability is important

GIF (.gif)

Best for: Simple animations, basic graphics

Pros: Animation support, wide compatibility

Cons: Limited colors, larger files

Use when: Simple animations needed

Complete Image Implementation

Explore comprehensive image implementation with accessibility, responsive design, and interactive features:

Code Editor

<figure>
  <img
    src="https://picsum.photos/id/1015/600/400"
    alt="A river winding through a green valley"
    width="600" height="400"
    loading="lazy">
  <figcaption>Always describe the image in alt.</figcaption>
</figure>

<!-- Decorative image: empty alt so screen readers skip it -->
<img src="https://picsum.photos/id/1025/120/80" alt="" width="120" height="80">

Preview

Image Accessibility

Alt Text Best Practices

  • • Be descriptive but concise (under 125 characters)
  • • Describe the content and function
  • • Avoid "image of" or "picture of"
  • • Use empty alt="" for decorative images
  • • Include relevant context
  • • Don't repeat surrounding text

Image Categories

Informative:

Convey information - require descriptive alt text

Decorative:

Visual enhancement only - use alt=""

Functional:

Clickable images - describe action

Alt Text Examples

❌ Poor Examples:

alt="image"

Too generic

alt="Picture of a dog running in the park on a sunny day with green grass and blue sky"

Too long

✅ Good Examples:

alt="Golden retriever running in park"

Descriptive and concise

alt=""

For decorative images

Responsive Images

CSS Responsive Images

img {
  max-width: 100%;
  height: auto;
}

/* Container approach */
.image-container {
  width: 100%;
  max-width: 600px;
}

Simple CSS solution that works for most cases. Images scale down but maintain aspect ratio.

Picture Element

<picture>
  <source media="(min-width: 800px)" 
          srcset="large.jpg">
  <source media="(min-width: 400px)" 
          srcset="medium.jpg">
  <img src="small.jpg" alt="Description">
</picture>

Serves different images based on screen size, providing optimal file size for each device.

Image Performance Optimization

Lazy Loading

loading="lazy"

Defers image loading until they're about to enter the viewport, improving initial page load.

Preloading

loading="eager"

Loads images immediately for above-the-fold content that's critical for user experience.

Compression

• Use appropriate quality settings

• Choose optimal file formats

• Implement progressive JPEG

• Consider WebP for modern browsers

Image Best Practices

✅ Best Practices:

  • Always include meaningful alt text
  • Optimize file sizes for web delivery
  • Use responsive images for different screens
  • Implement lazy loading for performance

❌ Common Mistakes:

  • • Missing or generic alt text
  • • Using images for text content
  • • Not optimizing file sizes
  • • Ignoring mobile responsiveness
  • • Loading all images at once
  • • Using wrong file formats

HTML Images — step by step

1 / 4
1

Add an img element

The img element embeds an image. It requires a src attribute (the image URL) and an alt attribute (a text description for accessibility and SEO).

1 of 4
💡The loading='lazy' Attribute
Add loading='lazy' to images that are below the fold. Browsers will defer downloading them until the user scrolls near them, reducing initial page load time and data usage significantly.

🎯 Quick Check

What should you write in the alt attribute for a purely decorative image?

HTML Images: summary

Key points

  • alt is required: screen readers read it, and it shows if the image fails to load. Use alt="" for purely decorative images.
  • WebP and AVIF are much smaller than JPEG or PNG for the same quality.
  • Don't lazy-load the main image at the top of the page (the LCP image); it should load immediately.
  • Use <figure> and <figcaption> to add a visible caption.

Frequently asked questions

›What should I write in the alt attribute?

Describe what the image shows or does in context, briefly, as you would to someone on the phone. Don't start with "image of".

›Why is my image not showing?

Usually the path in src is wrong (check capitalisation and folder) or the file isn't where the page expects it. Open DevTools → Network to see the 404.

›What image format should I use?

WebP or AVIF for photos, SVG for logos and icons, and PNG only when you need lossless quality and those formats aren't an option.