Responsive images can be implemented in various ways using HTML and CSS. Here are three commonly used techniques:
<img src="image.jpg" srcset="image.jpg 1x, image@2x.jpg 2x, image@3x.jpg 3x">
<picture>
<source srcset="small.jpg" media="(max-width: 600px)">
<source srcset="medium.jpg" media="(max-width: 900px)">
<img src="large.jpg" alt="Responsive Image">
</picture>
<div class="image"></div>
@media (max-width: 600px) {
.image {
background-image: url('small.jpg');
background-size: cover;
}
}
@media (max-width: 900px) {
.image {
background-image: url('medium.jpg');
background-size: cover;
}
}
@media (min-width: 901px) {
.image {
background-image: url('large.jpg');
background-size: cover;
}
}
These techniques provide the flexibility needed to ensure that your images look great on different devices and screen sizes. Experiment with them and find the approach that works best for your project!