Responsive Blog card using css variables and media queries

Solution retrospective
Utilising the css variables and media queries. Next time I will try to make variable selectors more efficent with less code. Also will focus on building mobile first in future
What challenges did you encounter, and how did you overcome them?Using semantic elements to improve accessiblity. To help I looked at best pracices online
What specific areas of your project would you like help with?Any image sizing tips?
Please log in to post a comment
Log in with GitHubCommunity feedback
- P@huyphan2210
Hi, @amancalledkidd
I would like to answer your question regarding image sizing.
- Your
img
'sheight
doesn't fill its parent element
You’re setting your
img
'swidth
to100%
, making it match thewidth
of its parent element (.blog-post-header
). That works as expected.However, the
img
's height doesn’t fill the.blog-post-header
. This happens because images are inline-replaced elements by default, meaning they behave like text characters. As a result, browsers leave extra space below the image, similar to how they leave space below letters like "g" or "p" in text.To fix this, you can set
vertical-align: top
on theimg
. This works becausevertical-align
adjusts how inline-replaced elements align relative to the text baseline. By settingvertical-align: top
, you align the image to the top of its container, removing the unwanted space below it.- Use the
picture
Element withsource
andimg
This challenge has a quite straightforward solution. However, you might face more complex challenges that require better image handling. In such cases, consider using the picture element to serve different images based on viewport size.
<picture> <source srcset="image-large.jpg" media="(min-width: 768px)"> <source srcset="image-medium.jpg" media="(min-width: 480px)"> <img src="image-small.jpg" alt="Responsive example image"> </picture>
How It Works:
- The browser checks each
<source>
element from top to bottom. - It uses the first
srcset
that matches the viewport's media query. - If none match, the
<img>
element serves as a fallback.
Marked as helpful - Your
- P@amancalledkidd
Thank you very much @huyphan2210. This advice is really helpful! I had no idea there was a picture element, will be putting this into practice on the next challenge - Thank you again!
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