Filter (Advanced Search)
Downloads
3
Views
309
Video

The picture Element | Images

Details

The HTML <picture> element gives web developers more flexibility in specifying image resources.

The <picture> element contains one or more <source> elements, each referring to different images through the srcset attribute. This way the browser can choose the image that best fits the current view and/or device.

Each <source> element has a media attribute that defines when the image is the most suitable.

Example

Show different images for different screen sizes:

<picture>
  <source media="(min-width: 650px)" srcset="/images/cat.jpg">
  <source media="(min-width: 465px)" srcset="/images/computer_lab.jpg">
  <img src="/images/computer_lab.jpg" style="width:auto;">
</picture>


<!DOCTYPE html>
<html>
<head>
<title>The picture Element</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<b>The picture Element</b>
<p style="color:red">Images Show on Dimension in pixels</p>
<hr>
710px+ between 710px = cat.jpg<br>
709px between 565px = computer_lab.jpg<br>
564px between 0px = laptop.jpg<br>

<picture>
  <source media="(min-width: 710px)" srcset="/images/cat.jpg">
  <source media="(min-width: 565px)" srcset="/images/computer_lab.jpg">
  <img src="/images/laptop.jpg" style="width:auto;">
</picture>

<p>Resize the browser to see different versions of the picture loading at different viewport sizes.
The browser looks for the first source element where the media query matches the user's current viewport width,
and fetches the image specified in the srcset attribute.</p>

<p>The img element is required as the last child tag of the picture declaration block.
The img element is used to provide backward compatibility for browsers that do not support the picture element, or if none of the source tags matched.
</p>

<p><strong>Note:</strong> The picture element is not supported in IE12 and earlier or Safari 9.0 and earlier.</p>

</body>
</html>



Note: Always specify an <img> element as the last child element of the <picture> element. The <img> element is used by browsers that do not support the <picture> element, or if none of the <source> tags match.

 

 

When to use the Picture Element

There are two main purposes for the <picture> element:

1. Bandwidth

If you have a small screen or device, it is not necessary to load a large image file. The browser will use the first <source> element with matching attribute values, and ignore any of the following elements.

2. Format Support

Some browsers or devices may not support all image formats. By using the <picture> element, you can add images of all formats, and the browser will use the first format it recognizes, and ignore any of the following elements.

Example

The browser will use the first image format it recognizes:

<picture>
  <source srcset="/images/cat.png">
  <source srcset="/images/computer_lab.jpg">
  <img src="img_beatles.gif" alt="Beatles" style="width:auto;">
</picture>

Ad