Changing image src depending on screen size

This one is working for me. I don’t know if you’re familiar with this tag but it doesn’t even need CSS.

<picture>
    <source media="(min-width: 900px)" srcset="https://stackoverflow.com/BigImage.png">
    <source media="(min-width: 480px)" srcset="https://stackoverflow.com/MediumImage.png">
    <img src="https://stackoverflow.com/questions/30460681/OtherImage.png" alt="IfItDoesntMatchAnyMedia">
</picture>

In this case “BigImage” Would show when the device width is more than 900px. “MediumImage” when its’s more than 480px and “OtherImage” If It’s less than 480px. If you had “max-width: 900px” instead of min-width, it would also show when the width is more than 900px.

Hope it helps!

Leave a Comment