Responsive Product Preview Card Component
Design comparison
Solution retrospective
How do you go about rendering one image for mobile and another for larger screens?
I've tried two different methods (<picture><source> in html as well as with basic JavaScript), but I haven't found the ideal solution. You can see more in my readme for this build as well as my previous Frontend Mentor build linked in the readme.
Thanks!
Community feedback
- @hitmorecodePosted about 1 year ago
The picture element should work just fine, in case you want a work around you can do something like this
html
<img id="product-image-desktop" alt="Perfume bottle" src="./Project Requirements/images/image-product-desktop.jpg"> <img id="product-image-mobile" alt="Perfume bottle" src="./Project Requirements/images/image-product-mobile.jpg">
css
**When on desktop #product-image-mobile { display: none; } **when on mobile #product-image-mobile { display: block; } #product-image-desktop { display: none; }
You don't need to use javascript for this
2@crwainstockPosted about 1 year agoThanks so much! @hitmorecode
I'll give this a try. I know it should be possible without JS, but the html option just wasn't working the way I was doing it.
I appreciate your help!
0 - @retr0webPosted about 1 year ago
Hi again Crystal!
About picture method, were you using media queries to set the source of the image?
I found it hard to work with picture before, but after doing some research it's a nice way to make responsive image.
The syntax looks like this:
<picture> <source media="(min-width: 1200px)" srcset="wide-crop.jpg"> <img src="close-crop.jpg" alt="…"> </picture>
This method helped me through FAQ accordion challenge.
Here is a link more about using this method.
0@crwainstockPosted about 1 year agoThanks! I tried doing it this way, but it wasn't quite working. Only one image was rendering no matter what the screen size was. I got it working using a similar method to what the previous person commented here.
I appreciate the help! @retr0web
1
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