Skip to content

01. Images

Images are essential for creating visually appealing and informative web pages. The HTML <img> element embeds images into web documents. Understanding how to properly use images, including attributes like src, alt, and responsive techniques, is crucial for creating accessible and performant websites.

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

Image source URL:

<img src="photo.jpg" alt="Photo">
<img src="/images/photo.jpg" alt="Photo">
<img src="https://example.com/image.jpg" alt="Photo">

Alternative text (required for accessibility):

<img src="logo.png" alt="Company Logo">

Specify dimensions:

<img src="photo.jpg" alt="Photo" width="300" height="200">

Lazy loading:

<img src="photo.jpg" alt="Photo" loading="lazy">

Responsive images:

<img src="photo.jpg"
srcset="photo-small.jpg 300w, photo-large.jpg 800w"
sizes="(max-width: 600px) 300px, 800px"
alt="Photo">
<!-- Good: Descriptive alt text -->
<img src="sunset.jpg" alt="Beautiful sunset over the ocean">
<!-- Avoid: Missing or empty alt -->
<img src="sunset.jpg">
<img src="sunset.jpg" alt="">
  • Use appropriate formats (WebP, JPEG, PNG)
  • Compress images
  • Use responsive images
  • Consider lazy loading