
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - <picture> Tag
Introduction to <picture> Tag
The HTML <picture> tag defines different images for various sizes or resolution. It serves as an alternative to the <img> tag and includes both <source> and <img> elements. The <picture> tag is useful for displaying different images based on the device.
The <picture> element contains one or more <source> elements and one <img> describes the size and other attributes of the images. The browser evaluates each <source> element and loads the most appropriate image. If no matches are found, the browser displays the image specifies by the <img> tag.
Use of <picture> Tag
Instead of scaling image according to the view port width, the <picture> element allows for the specification of multiple images that better fit the browser view port. It can be used for the following purposes −
- To adjust and crop images for different media situations.
- To provide alternative image formats when a certain format isn't supported.
How to Use <picture> Tag?
You can use the <picture> tag to define different images based on various media rules. This requires multiple <source> elements and one <img> element.
Syntax
The following is the syntax of <picture> tag:
<picture> <source media="..." srcset=".."> <source media="..." srcset=".."> <img src="..." alt=".."> </picture>
Attributes
The HTML <picture> tag also supports the following additional attributes −
S.No. | Attribute & Description |
---|---|
1 | Accepts any valid media query that would typically be defined in CSS. |
2 | Defines a single width descriptor, a single media query with a width descriptor, or a comma-delimited list of media queries with a width descriptor. |
3 | Specifies the image URL. |
4 | (Required) Specifies the URL of the image to use in various situations. |
5 | Defines the MIME type. |
Example: Basic Usage
The following example demonstrates how to specify different images based on screen width using the media attribute:
<!DOCTYPE html> <html> <style> body { text-align: center; } </style> <body style="background-color:#EAFAF1"> <picture> <source media="(min-width: 600px)" srcset="/artificial_intelligence/images/artificial-intelligence-mini-logo.jpg"> <source media="(min-width: 450px)" srcset="/cg/images/logo.png"> <img src="/html/images/html-mini-logo.jpg" alt="LOGO" style="width:auto;"> </picture> </body> </html>
Example: Responsive Image for Retina
In the below example, demonstrates how to set the different images for retina displays. When the code is executed, "html-mini-logo.jpg" will be loaded on retina displays, while "logo.png" will be loaded on regular displays."
<!DOCTYPE html> <html> <style> body { text-align: center; } </style> <body style="background-color:#EAFAF1"> <picture> <source srcset="/html/images/html-mini-logo.jpg 2x, /cg/images/logo.png 1x"> <img src="/html/images/html_images.jpg" alt="High-resolution image"> </picture> </body> </html>
Example: Image Formats
Here, we demonstrate how to specify different formats. When executed, if the browser supports WebP, it will load 1.webp a more optimized format. Otherwise, html_images.jpg will be loaded.
<!DOCTYPE html> <html> <style> body { text-align: center; } </style> <body style="background-color:#EAFAF1"> <picture> <source type="image/webp" srcset="https://www.gstatic.com/webp/gallery/1.webp"/> <source type="image/jpeg" srcset="/html/images/html_images.jpg"/> <img src="/html/images/html-mini-logo.jpg" alt="Different formats images"/> </picture> </body> </html>