everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

Maximizing Image Customization with HTML Picture Tag

  1. What is <picture>
    1. Basic Rules
  2. What can <picture> do
    1. media rule
      1. Example 1.1: Customize Image Sources
      2. Example 1.2: Customize Image Size
    2. Pixel Density Descriptor in srcset
    3. Image type
  3. Browser compatibility

What is <picture>

the HTML <picture> tag allowed developers to have the flexibility to specify dedicated image resources in different scenarios, it provides a solution for responsive images in web design. It allows developers to specify different sources for an image, depending on the screen size and other factors, such as screen density and viewport size.

HTML property<picture>
Child element<source srcset> , <img src>
<picture>
    <source type="image/avif" srcset="img/banner.avif">
    <source type="image/webp" srcset="img/banner.webp">
    <img src="img/banner.jpg">
</picture>

Basic Rules

The <picture> tag is working similar as the HTML <audio> and <video> tags, the first <source> that fits the preference will be selected and rendered.

As a fallback option, if none of the sources are matched, or when the browser does not support <picture>, <img src> will be rendered.

Every <picture> element must contain <img src> descendant, without the declaration, <picture> will not be working.


What can <picture> do

1. media rule
Example 1.1: Customize Image Sources

Use media and scrset to apply different image sources for different viewport sizes, for example:

<picture>
    <source srcset="img/banner3x.jpg" media="(min-width: 1920px)">
    <source srcset="img/banner2x.jpg" media="(min-width: 1024px)">
    <img src="img/banner.jpg"  />
</picture>

In this example

  1. When the browser width is <= 1920px, banner3x.jpg will be rendered
  2. When the browser width is <= 1024px, banner2x.jpg will be rendered
  3. Otherwise, banner.jpg will be rendered
Example 1.2: Customize Image Size

Similar to example 1.1, but other than specifying image sources themselves, you can also specify how big you want your images to be displayed.

<picture>
  <source srcset="img/banner.jpg" media="(orientation: portrait)" width=100% />
  <img src="img/banner.jpg" width=50% />
</picture>

In this example

  1. When the viewport orientation is a portrait, banner.jpg will be rendered in width 100%
  2. Otherwise, banner.jpg will be rendered in width 50%
2. Pixel Density Descriptor in srcset

This will allow developer to have option to define a different image sources to serve in different DPI screen, for example, you can use a high-res image in high-DPI screen.

<picture>
  <source srcset="pic-lg.png 1x" media="(min-width: 2560px)">
  <source srcset="pic-md.png 1x, pic-lg.png 2x" media="(min-width: 1920px)">
  <img src="pic-sm.png" />
</picture>

In this example

  1. If your viewport width is >= 2560px & screen’s pixel density is 1x, pic-lg.png will be rendered
  2. If your viewport width is >= 1920px & screen’s pixel density is 1x, pic-md.png will be rendered
  3. If your viewport width is >= 1920px & screen’s pixel density is 2x, pic-lg.png will be rendered
  4. Otherwise, the rules declared in <img src> will be used (pic-sm.png)

Note: The display density descriptor will choose the image base on the screen’s pixel density (for eg: 1x, 1.5x, 2x,…), not the image’s actual size.

3. Image type
<picture>
    <source type="image/avif" srcset="img/banner.avif">
    <source type="image/webp" srcset="img/banner.webp">
    <img src="img/banner.jpg">
</picture>

In this example

  1. If your browser supports AVIF format, the 1st source (banner.avif) will be selected and rendered. Otherwise, it will move to the second source.
  2. If your browser supports WebP format but not AVIF, the 2nd source (banner.webp) will be selected and rendered.
  3. If your browser does not support both AVIF and WebP format, it will move to the final fallback option, which is defined in <img src>

If you are interested to explore more about AVIF and WebP:


Browser compatibility

DesktopMobile
Chrome 38
Edge 13
Safari 9.1
Firefox 38
Opera 25
Internet Explorer
Chrome Android 38
WebView Android 38
iOS Safari 9.3
Firefox Android 38
Opera Android 25
Samsung Internet 3.0

Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture#browser_compatibility

Summary

The HTML <picture> tag provides a powerful and flexible solution for responsive images, allowing you to deliver the best possible image for each device and screen size. With this element, you can ensure that your images look great on all devices, regardless of screen size or screen density.

When to use <picture>

When you want to specify preferences, for example:

Use a portrait design banner when the device orientation is in portrait view, or vice versa

Use a high-res image on a high-DPI screen, or vice versa

Use a large-size banner on a big screen, or vice versa

When you want to use a modern image type like AVIF or WebP without worrying about compatibility issues

By specifying which image to use in , you can ensure only the preference image sources will be downloaded


Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

$5.00
$10.00
$15.00

Or enter a custom amount

$

Thank you for your contribution!

Donate

Topics:

CSS (27) css pseudo (3) debug (2) design (4) fonts (2) html (6) image (8) layout (5) optimization (10) performance (9) responsive (8) Software-development (1)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements
%d bloggers like this: