HTML Picture Element

While developing a web page, we come across situations where depending on the screen size or device, we need to display different images at a particular place on the page.

The HTML <picture> element is the solution to such problems. It allows us to specify a set of images using the <source> tags.

Depending on the device or screen size, while rendering the page, the browser selects the first appropriate image from the <picture> element and displays it on the screen.

This feature is helpful in responsive web design, where the layout of a page adjusts according to the device screen size.

Let's take an example and understand the implementation of <picture> element.

HTML Code
<picture>
<source srcset="small.jpg" media="(max-width: 500px)">
<source srcset="medium.jpg" media="(max-width: 700px)">
<img src="large.jpg" alt="Large Image">
</picture>

Inside the opening <picture> and closing </picture> tags, we can specify multiple images using the <source> tags.

The <source> tag has two attributes srcset and media.

The srcset attribute specifies the URL of the image, and the media attribute specifies the condition.

The condition helps the browser in deciding the image to be used.

As in the example above, the browser will use the image small.jpg on devices with a screen width of 500px or less.

Similarly, the browser will use medium.jpg on devices with a screen width of 700px or less.

The browser checks the <source> tags from top to bottom and ignores further <source> tags if a condition mentioned in the media attribute is matched.

The <source> element is an empty HTML element, which means it does not have a closing tag.

When all <source> tags are defined, we define an alternate image using the <img> tag.

The browser uses this image when no other image is used from the <source> tags.

Also, the <img> element is used as a fallback for browsers that don't support the <picture> element.

Hence we should always place the <img> tag in the last. Otherwise, an unexpected view can occur.

Let's see a complete example. Click on 'Result on Editor' and view the result of the code.

Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Picture Element Example</title>
</head>
<body>

<h1>HTML Picture Element</h1>

<picture>
<source srcset="small.jpg" media="(max-width: 500px)">
<source srcset="medium.jpg" media="(max-width: 700px)">
<img src="large.jpg" alt="Large Image">
</picture>

<p>Resize the browser to see the loading of different images.</p>
<p>If the screen width is 500px or less then the image small.jpg will load.</p>
<p>If the screen width is 700px or less then the image medium.jpg will load.</p>
<p>If the screen width is more than 700px then the image large.jpg will load.</p>

</body>
</html>

Advantages of Picture Element

The large-size images consume more bandwidth and take time to download. It can increase the loading time of a website.

The users of mobile phones and other handheld devices may not require large images.

And hence by using <picture> element, we can provide an option to the browser to load an alternate smaller image for such devices.

Images can be of different formats. Some devices or browsers may not support all image formats.

Hence, in that case, using <picture> element, we can provide multiple images in different formats. The browser will use the first recognised format and ignores the rest.

HTML Code
<picture>
<source srcset="chromelogo.png">
<source srcset="chromelogo.apng">
<source srcset="chromelogo.gif">
<img src="chromelogo.jpg" alt="Google Chrome Logo">
</picture>
Example
<!DOCTYPE html>
<html>
<head>
<title>Different File Formats in Picture Element</title>
</head>
<body>

<h1>Different File Formats in Picture Element</h1>

<picture>
<source srcset="chromelogo.png">
<source srcset="chromelogo.apng">
<source srcset="chromelogo.gif">
<img src="chromelogo.jpg" alt="Google Chrome Logo">
</picture>

<p>The browser always selects the first recognized format and ignores the rest images in picture element.</p>
<p>If the browser supports PNG files then first image chromelogo.png will be displayed. And other images will be ignored by the browser.</p>

</body>
</html>

The HTML <picture> element can help us display different images based on the user's operating system, location, language and browser.


  • Disclaimer: ResultUniversity.com is an independent private website and it is in no way affiliated to any Indian Government official website. The sole purpose of this website is to provide notifications regarding the latest results published. Though utmost care is being taken, we can not guarantee 100% correctness of information. Using this website confirms that you agree to all the terms and conditions.