I want to render a different svg based on the browser screen width. I've found a solution that expects me to use srcSet
within an img tag. However, I can't get it to work.
The goal should be to render SmallTalents
on mobile, and BigTalents
on tablets+
Quick notes:
import React from "react";
import SmallTalents from "../../../assets/smallTalents.svg";
import BigTalents from "../../../assets/bigTalents.svg";
function Example() {
return (
<section>
<img
srcSet={`${SmallTalents}, 200w ${BigTalents} 600w`}
sizes="(max-width: 600px) 480px, 800px"
src={SmallTalents}
alt="talents"
/>
</section>
);
}
export default Example;
Tried this, not working
<picture>
<source media="(min-width: 300px)" srcset={SmallTalents} />
<source media="(min-width: 900px)" srcset={BigTalents} />
</picture>
<img src={SmallTalents} alt="talents" className="my-5 md:hidden" />
<img src={BigTalents} alt="talents" className="my-5 hidden md:flex" />
You can use picture element in html for rendering different images based on the screen size:
<picture>
<source media="(min-width: 650px)" srcset="img_food.jpg">
<source media="(min-width: 465px)" srcset="img_car.jpg">
<img src="img_girl.jpg">
</picture>
I think there is a small typo in your srcSet property, the 200w should be on the left side of the comma. i.e srcSet={
${SmallTalents} 200w, ${BigTalents} 600w}