This is a solution to the Product preview card component challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.
Users should be able to:
- View the optimal layout depending on their device's screen size
- See hover and focus states for interactive elements
- Semantic HTML5 markup
- CSS custom properties
- Flexbox
- Mobile-first workflow
- BEM methodology
I learned how to use <picture>
element for displaying different images based on different screen sizes.
And use of <picture>
element reduces the work amount, complexity and use of media queries (@media
)
example :
example 1
<picture>
<source media="(min-width: 600px)" srcset="./image/desktop-view.png" />
<img src="./image/mobile-view.png" alt="Perfume product picture" />
</picture>
example 2
<picture>
<source media="(min-width: 768px)" srcset="./image/desktop-view.png" />
<source media="(min-width: 375px)" srcset="./image/tablet-view.png" />
<img src="./image/mobile-view.png" alt="Perfume product picture" />
</picture>
In above example 2,
- if the screen is a mobile screen (375px wide or less), then you see the mobile-view.png
- if the screen is a tablet screen (768px wide or less), then you see the tablet-view.png
- and if it is large screen (greater than 768px), then you can see the desktop-view.png
- Frontend Mentor - @Des2Dev