CSS Tutorial
Responsive images ensure that images display optimally on different screen sizes and devices, enhancing user experience and website performance.
<picture>
Element: A container that holds various image sources for different scenarios.srcset
Attribute: Specifies multiple image sources and their corresponding display widths.<picture>
Element: Replace img
with <picture>
.<picture>
, add <source>
elements for each image source, using the srcset
attribute to specify the image width range.<img>
element with the default image as a fallback for unsupported browsers./* Define breakpoints */
@media (min-width: 768px) {
/* Use the large image above 768px */
.image {
src: url("large.jpg") 768w;
}
}
@media (min-width: 1200px) {
/* Use the extra large image above 1200px */
.image {
src: url("x-large.jpg") 1200w;
}
}
By implementing responsive images, websites can optimize images for different devices and screen sizes, improving performance and providing a better user experience.