<picture> Tag in HTML

The <picture> tag in HTML is used to define multiple sources for an image, providing responsive image support. It allows the browser to choose the appropriate image based on the device's screen size, resolution, and other factors. The <picture> tag is used with one or more <source> elements, each specifying a different image file and a media query.

Key Points on <picture> Tag:

Syntax of <picture> Tag:

Syntax Example

<picture>
            <source media="(min-width: 800px)" srcset="large-image.jpg">
            <source media="(min-width: 400px)" srcset="medium-image.jpg">
            <img src="small-image.jpg" alt="A responsive image">
        </picture>

Example of <picture> Tag in HTML:

This example demonstrates how to use the <picture> tag for responsive images that adapt to different screen sizes.

Code Example

<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>HTML <picture> Tag Example</title>
        </head>
        <body>
        
            <h1>Responsive Image Using <picture> Tag</h1>
        
            <picture>
                <source media="(min-width: 800px)" srcset="large-image.jpg">
                <source media="(min-width: 400px)" srcset="medium-image.jpg">
                <img src="B1.jpg" alt="Responsive Image">
            </picture>
        
        </body>
        </html>

Output

Responsive Image Using picture Tag

Sample Image

The <picture> tag is a powerful tool for creating responsive images in HTML. By using different <source> elements with media queries, it ensures that the most appropriate image is displayed based on the device's characteristics, such as screen size and resolution. This tag is ideal for optimizing the user experience across a range of devices.