Categories
Tags
a Accessibility Advanced Algorithms Alias Bash bash Basics Beginners Best Practices Big O bun cat cd CLI Comments Container Elements Container Queries cp css CSS Data Structures deno Doctype Download Editors Error Examples Features figure File Watching Filesystem fish Fish Shell footer frontend Guide header Hello World History Homebrew HTML HTML5 humor img javascript JavaScript Learning less Links linux Linux ls macOS Media Queries meta Mobile-First mv Netcat Networking Node.js npm Package Manager picture pm2 Productivity programming Programming pwd Responsive Design Responsive Images rmdir Scalability section Semantic HTML shell Shell Shell Script Shells srcset State Management Structure Svelte Svelte Store SvelteKit svg Tables tail Text Formatting Tools touch Troubleshooting Tutorial Unix ux Vim web development Web Development web-development webdev
170 words
1 minutes
How to Use the `picture` Tag in HTML
How to Use the picture
Tag in HTML
The <picture>
tag in HTML is used to provide multiple sources for an image, allowing for responsive images that adapt to different screen sizes and resolutions. This guide explains how to use the <picture>
tag.
Basic Syntax
The <picture>
tag contains one or more <source>
elements and an <img>
element:
<picture>
<source srcset="image-large.jpg" media="(min-width: 800px)">
<source srcset="image-medium.jpg" media="(min-width: 400px)">
<img src="image-small.jpg" alt="Description of the image">
</picture>
Use Cases
Responsive Images
The <picture>
tag is commonly used to provide responsive images that adapt to different screen sizes:
<picture>
<source srcset="image-large.jpg" media="(min-width: 800px)">
<source srcset="image-medium.jpg" media="(min-width: 400px)">
<img src="image-small.jpg" alt="Description of the image">
</picture>
Art Direction
You can use the <picture>
tag to provide different images for different contexts, such as different crops or compositions:
<picture>
<source srcset="image-landscape.jpg" media="(orientation: landscape)">
<source srcset="image-portrait.jpg" media="(orientation: portrait)">
<img src="image-default.jpg" alt="Description of the image">
</picture>
Conclusion
The <picture>
tag is a powerful tool for providing responsive images and art direction in HTML. By using the <picture>
tag, you can ensure that your images look great on all devices and screen sizes.
How to Use the `picture` Tag in HTML
https://zxce3.net/posts/how-to-use-the-picture-tag-in-html/