HTML responsive images are images that adjust to different screen sizes, devices, and display conditions. They help websites display images correctly on desktop computers, tablets, and mobile phones.
Responsive images are important for modern web development because visitors use different devices to access websites. By using HTML attributes such as srcset and sizes, the <picture> element, and CSS properties such as max-width, developers can improve image display and loading performance.
What Are Responsive Images in HTML?
Responsive images adapt to the available space or allow the browser to select a suitable image for the user’s device. For example, a website may display a large image on a desktop and a smaller version on a mobile phone.
HTML provides different techniques for creating responsive images, depending on whether you need flexible dimensions, different image resolutions, or different image crops.
Making Images Responsive Using CSS
The simplest way to make an image responsive is to use CSS. The max-width property prevents an image from becoming wider than its container.
HTML Responsive Image Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Responsive Images</title>
<style>
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<h1>HTML Responsive Images</h1>
<img src="landscape.jpg"
alt="A beautiful mountain landscape"
width="800"
height="500">
</body>
</html>
In this example, max-width: 100% ensures that the image does not exceed its container’s width. The height: auto property preserves the original aspect ratio.
Using the srcset Attribute
The srcset attribute allows you to provide multiple versions of an image at different resolutions. The browser can select an appropriate version based on the device’s display characteristics.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw,
(max-width: 1000px) 80vw,
800px"
alt="A mountain surrounded by green trees"
width="800"
height="500">
Here, srcset lists available image files and their intrinsic widths. The sizes attribute tells the browser how much space the image is expected to occupy at different viewport widths. The browser uses this information, along with factors such as display resolution, to select an appropriate source.
Using the Picture Element for Responsive Images
The HTML <picture> element is useful when you want to display different images depending on screen size. This technique is called art direction.
<picture>
<source
media="(max-width: 600px)"
srcset="portrait.jpg">
<source
media="(min-width: 601px)"
srcset="landscape.jpg">
<img
src="landscape.jpg"
alt="A scenic mountain view"
width="800"
height="500">
</picture>
In this example, the browser can select a portrait image for smaller screens and a landscape image for larger screens. The <img> element provides the fallback image and alternative text.
Responsive Images Attributes and Properties
| Attribute or Property | Purpose |
|---|---|
src | Specifies the default image source. |
srcset | Provides multiple image sources. |
sizes | Describes the expected rendered image width for source selection. |
media | Defines the conditions for selecting a <source> element. |
max-width | Prevents an image from exceeding its container’s width. |
height: auto | Maintains the image’s aspect ratio when its width changes. |
alt | Provides alternative text for accessibility. |
Related HTML Tutorials
Continue learning about HTML images with these tutorials on Web4learners:
- Learn the fundamentals of displaying images in the HTML Images tutorial.
- Understand the attributes used to control images in the HTML Image Attributes tutorial.
- Learn how to choose different images for different conditions in the HTML Picture Element tutorial.