Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

CSS FlexBox, JS

_SabijaThor_β€’ 355

@SasaVatic

Desktop design screenshot for the Stats preview card component coding challenge

This is a solution for...

  • HTML
  • CSS
1newbie
View challenge

Design comparison


SolutionDesign

Solution retrospective


I'm curious.. for card image should I use <img> element or background: url(./../..) css property. Because when setting image via <img> src attribute I used js to switch between desktop and mobile version of image

Community feedback

Faris Pβ€’ 2,810

@FarisPalayi

Posted

Hey, nice job on this oneπŸ‘.

You don't need to use js to change the image to make it responsive.

  • You can use the html picture tag with the source tag, and you can set a media query breakpoint using media attribute. It would look something like this:
<picture>
  <source media="(min-width:920px)" srcset="desktop-img.png">
  <source media="(max-width:400px)" srcset="mobile-img.jpg">
  <img src="fallback-img.jpg" alt="image">
</picture>

Or you can use the srcset attribute on the img tag itself.

Learn more: Link-1, Link-2

  • And also, if you wanna use media queries in js, instead of using offsetWidth, you can use the native window.matchmedia() method. Like this:
const mediaQuery = window.matchMedia( '( min-width: 768px )' )

if ( mediaQuery.matches ) {
  console.log('Media Query Matched!')
}

Learn more: A Complete Guide to CSS Media Queries - css-tricks, window.matchmedia() - MDN

Marked as helpful

1

_SabijaThor_β€’ 355

@SasaVatic

Posted

@FarisPalayi I appreciate your feedback. It helped me improve my code, thank you!

1

Please log in to post a comment

Log in with GitHub
Discord logo

Join 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