Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 1 year ago

Product Preview Card with HTML and CSS

Grunt395•40
@Grunt395
A solution to the Product preview card component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


I used Flexbox for the first time in this project and struggled to center the main content of the page. Is there a best way to center the content with or without Flexbox?

This project involved a desktop and a mobile image included. I ended up displaying both images at the same time on the page, but would hide one of them using display: none; for the respective layout. Is there a better way to display either the desktop or mobile image without having to manually hide one of them?

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Anar•700
    @anar-sol
    Posted over 1 year ago

    Hello =)

    Instead of using two separate img elements, you can use picture and source to specify different images for mobile and desktop depending on the viewport size.

    In my solution, I've set the mobile version as the default image and the desktop one starting at 640px width viewport.

    <picture>
        <source media="(min-width: 640px)" srcset="images/image-product-desktop.jpg" width="600" height="900">
        <img src="./images/image-product-mobile.jpg" alt="Gabrielle Essence Eau De Parfum bottle" width="686" height="480" class="card__image">
    </picture>
    

    As you'll see in these MDN and web.dev articles, there are many other situations where picture and source can be useful.

    Marked as helpful
  • P
    Qendresa Braha•350
    @HiQendresa
    Posted over 1 year ago

    Hi @Grunt395 👋

    Congratulation on finishing this challenge, you did great👏🏻

    CSS Flexbox is great tool used in web development to create responsive and flexible layouts. 🚀

    You can learn more about it in this link

    Here are some suggestions to help improve your code:

    • It is not a very good idea to apply styles directly to the <body> selector because it is straightforward. Styles applied to the <body> selector can impact the entire page, making it suitable for setting background colors, fonts, and other global design choices. In this case you have only one container but in cases where there are more components in the page you will need to be more selective.

    So the selector body should be more general like this e.g:

    body { min-height: 100vh; text-rendering: optimizeSpeed; line-height: 1.5; overflow-x: hidden; max-width: 1440px; position: relative; }

    • A crucial element is missing in your code, which should always be included, whether it's a component or a full website, it is the main element. Its presence is essential for maintaining good semantics and accessibility, as it assists in identifying the primary content of your site.

    • You can set the content inside the <main></main> and give a max-width to the container.

    <body> <main> <div class="container"></div> </main></body>

    • To center the content, there are a lot of ways to do that. .parent-container { display: flex; justify-content: center; align-items: center; } or you can use ** margin: 0 auto;*** another way is using position absolute to the container.

    Wish you good luck! Happy Coding 😊

    Marked as helpful
  • Apurv Patel•20
    @apurvpatel2121
    Posted over 1 year ago

    use media query

    .image { width: 100%; height: 300px; /* Adjust the height as needed / background-size: cover; background-position: center; background-image: url('desktop-image.jpg'); / Default image */

    @media (max-width: 768px) { background-image: url('mobile-image.jpg'); /* Mobile image for screens 768px or smaller */ } }

    Marked as helpful
  • Mikail Ghali Imam•200
    @Imammika
    Posted over 1 year ago

    hello bro! you can just use one image tag next time, then use media queries to specify how you want the picture to look on smaller screens. all the best. Happy Coding.

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 all CSS, SCSS and Less files in your repository.

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.

How does the JavaScript validation report work?

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

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

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

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