Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted 16 days ago

product-preview-card-component-main

o-k-harmash•160
@o-k-harmash
A solution to the Product preview card component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

🖼️ Adaptive Images in Flex Context — Observations & Issues

When working with responsive (adaptive) images inside a flex layout, I've encountered a few non-intuitive behaviors that raised questions.

1. <img> with width: 100% doesn't always behave as expected inside flex children

Using the following setup:

.card__preview {
    flex: 1 1 50%;
}

...you'd expect that the <img> inside would stretch to fill its parent's width. But it often doesn't work unless the image is explicitly wrapped and its dimensions tightly controlled.
In my case, the <img> didn't respect the container’s calculated width — it either overflowed or collapsed unexpectedly depending on intrinsic dimensions.

2. Limiting image height via the wrapper doesn't always work

I tried to constrain the image height via the parent, but it didn’t work properly. The image would still overflow unless I set the height (or max-height) directly on the <img> element.

Example fix that worked:

.card__media {
    max-block-size: 20rem; /* hard limit on image height */
    inline-size: 100%;     /* stretch to parent width */
    object-fit: cover;     /* fill while maintaining aspect ratio */
}

Without this direct max-block-size on the image, the image simply ignored the parent's height restriction and rendered at its natural aspect ratio.


❓Is this expected?

From what I can tell:

  • ✅ Setting width: 100% on <img> inside a flex container only works properly if the parent itself has a definite width (from flex-basis or size constraint).
  • ❌ Limiting height on the image's parent is not enough unless the image itself is also explicitly constrained (via height, max-height, or block-size).
  • ✅ Using object-fit: cover helps maintain aspect ratio and cropping when the image is forced into constraints — but it needs both width and height to be explicitly managed.

✅ Best Practice Summary
  • Always wrap <img> inside a dedicated flex child if working in a flex container.
  • Set flex: 1 1 auto or flex: 1 1 50% on the wrapper, not the image.
  • Apply size constraints directly on <img>, not just the parent.
  • Use object-fit: cover + inline-size: 100% + max-block-size (or height) on the <img>.

This behavior seems to be standard CSS, but not very intuitive when you're aiming for pure "responsive" behavior with flex and img.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Duy Tran•110
    @DuyTM0508
    Posted 16 days ago

    Oke good

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
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit 1st-party linked stylesheets, and styles within <style> tags.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub