- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- 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 - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- 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 - 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 Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - 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
One or more <source> elements and one <img> element, which acts as the block's last child element, are contained within the <picture> element. Versions of a images for various display device scenarios are included in the <source> element. The size and other attributes of the images’s are described in the <img> element. The browser takes each of the child <source> elements and loads the most appropriate image. The browser shows the image given by the <img> tag if no matches are found.
Instead of having one image scaled according to the viewport width, the <picture> element permits the specification of multiple images that are intended to more accurately fill the browser viewport. It can be applied for the following purposes −
- to adjust and crop pictures for various media situations
- to provide substitute image formats when a certain format isn't supported.
Syntax
Following is the syntax of the <picture> tag −
<picture> Image and source tag </picture>
Specific Attributes
The HTML <picture> tag also supports the following additional attributes −
S.No. | Attribute & Description |
---|---|
1 | media Accepts any valid media query that would normally be defined in a CSS. |
2 | sizes Defines a single width descriptor, a single media query with width descriptor, or a comma-delimited list of media queries with a width descriptor. |
3 | src Specifies the URL of the image. |
4 | Srcset (required)Specifies the URL of the image to use in different situations. |
5 | type Defines the MIME type. |
Example
Let’s look at the following example, where we are going to use the basic <picture> tag.
<!DOCTYPE html> <html> <style> body { text-align: center; } </style> <body style="background-color:#EAFAF1"> <picture> <source media="(min-width: 600px)" srcset="https://www.tutorialspoint.com/artificial_intelligence/images/artificial-intelligence-mini-logo.jpg"> <source media="(min-width: 450px)" srcset="https://www.tutorialspoint.com/cg/images/logo.png"> <img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg" alt="LOGO" style="width:auto;"> </picture> </body> </html>
When we execute the above code, the output window will pop up, consisting of the three image sources used in the code, but it will display only one image on the webpage according to the viewport.
Example
Considering the other scenario where we are going to use the media attribute along with the <picture> tag, in this case we are going to take two image sources and check which one the browser displays.
<!DOCTYPE html> <html> <body style="background-color:#E8DAEF"> <picture> <source media="(max-width: 60px)" srcset="https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Flag_of_India.svg/300px-Flag_of_India.svg.png" /> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1e/CK_Nayudu_1930s.jpg/225px-CK_Nayudu_1930s.jpg" alt="ck nayudu" /> </picture> </body> </html>
On running the above code, it will generate an output consisting of the image of the CK Nayudu displayed on the webpage. In this case, we have chosen two image sources: one is the India flag and another is the CK Nayudu image, and the browser displays the CK Nayudu image on the webpage.
Example
Following the example where we are going to use the srcset attribute along with the <picture> tag.
<!DOCTYPE html> <html> <body style="background-color:#EBF5FB"> <picture> <source media="(min-width:650px)" srcset="https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/The_forest_near_Blatets%2C_Vinitsa.JPG/330px-The_forest_near_Blatets%2C_Vinitsa.JPG" /> <source media="(min-width:465px)" srcset="https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Cat_yawn_with_exposed_teeth_and_claws.jpg/330px-Cat_yawn_with_exposed_teeth_and_claws.jpg" width="1000px" height="200px" /> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/S%C3%A4ugende_H%C3%BCndin.JPG/330px-S%C3%A4ugende_H%C3%BCndin.JPG" alt="dog" style="width:auto;" /> </picture> </body> </html>
When we execute the above code, the output window will pop up and display the image according to the viewport based on the three image sources we used in the above code.
To Continue Learning Please Login
Login with Google