Product Preview Card using React and Styled Components
Design comparison
Solution retrospective
What is the best practice to swap the image based on a media query in React with Styled-Components? I couldn't get the image to swap without adding a second image and hiding the one that wasn't suppose to show. It works fine, just probably not optimal.
Community feedback
- @elaineleungPosted over 2 years ago
Hi Tim, about your question, you can try using one
img
element containing ansrcset
that allows the browser to swap the images based on breakpoint. More on this at MDN: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_imagesI also managed to use
picture
for responsive images when I was building another challenge using Next.js, but I thinkimg
may be more suited since it's only resolution switching that's going on here. Here's an example I used for this challenge:<img alt="perfume bottle" srcset="images/image-product-desktop.jpg 600w, images/image-product-mobile.jpg 686w" sizes="(min-width: 745px) 600px, 686px" src="images/image-product-desktop.jpg">
Also, I just checked out your site and the image looks a bit distorted in its aspect ratio; you may want to add an
object-fit: cover
to sort that out! 🙂Marked as helpful1@TimMartin13Posted over 2 years ago@elaineleung Do you know which browser Frontend Mentor uses? My app looks nothing like the screenshot they create based on my code in either Firefox nor Chrome.
0@elaineleungPosted over 2 years ago@TimMartin13 I think it's Firefox. When you say your code looks nothing like the screenshot, are you referring to the font family or something else? There's an issue commonly seen with the Fraunces font face in this challenge, where the optical size version looks different in the screenshot. I just checked the
index.html
file in your public folder and it looks like you've got that version; you can try copying a new link and unchecking the "optical size" box before doing so, and then see if you want to try the screenshot again.0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord