Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted 10 months ago

005-product-preview-card-component - HTML, CSS, Flexbox

P
tomhaeck•120
@tomhaeck
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?

CSS variables can be scoped globally by declaring them in a :root pseudo-element.

Do not forget to do a CSS reset for cross-browser compatibility. ::before and ::after are pseudo-elements, as compared to :root which is a pseudo-class.

*, *::before, *::after {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

The font properties that are generally used in your document should be declared as generally as possible, i.e. in the `` element.

Line height in percentage is the line height in pixels divided by the current font-size.

To center the product card in the element, the element is declared a CSS flexbox. There are then two ways to center the card within.

/* declaration of centering on the child element */
body {
  height: 100vh;
  display: flex;
}

.card {
  margin: auto;
}

/* declaration of centering on the parent element */
body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center; 
}

.card {
}

For an `` element, if you set either one of the CSS width or height to a fixed width, and the other one is set to auto, then the aspect ratio of the image is preserved. When both the CSS width and height are fixed, use object-fit: cover to make sure that the aspect ratio of the image is preserved and that it covers the full area.

Note that when spacing the items in the product info flexbox using justify-content: space-between, there is a small misalignment between our solution and the desktop design's jpg. This is solved by adding an ad hoc margin-top: -16px between product subtitle and product title.

Media queries are used to change the layout of the product card for mobile screens, e.g.

@media all and (max-width: 500px) {
    .product {
        flex-direction: column;
    }
}

The .attribution element is positioned absolutely with respect to the `` element, using position: absolute and bottom: 0.

What challenges did you encounter, and how did you overcome them?

When using Google fonts, you only need to import the Google font URL to have the font-family name available. Importing the font URL returns a stylesheet that has declared the font-family name for you. This is in contrast to when you want to import a local font file (e.g. .ttf) yourself.

/* Google font URL */
@import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap");

p {
  font-family: Montserrat, sans-serif;
}

/* local font file */
@font-face {
  font-family: whatever-name;
  src: url("assets/fonts/local-file.ttf");
}

p {
  font-family: whatever-name, sans-serif;
}

We use two different ways to vertically align inline elements with respect to one another:

.product-price-original {
  vertical-align: super;
}

/* vs */

.product-add-to-cart {
  display: flex;
  align-items: center;
}

Note that the src attribute of an `` element can be changed dynamically using CSS:

.product-image img {
  content: url("...");
}
Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • devkhrmnturk•60
    @devkhrmnturk
    Posted 10 months ago

    Hello, congratulations on completing the challenge, I took a look at your codes and I have a few suggestions for you, text decoration: line transition; Using the <s> or <del> tag directly instead can keep your code more semantic and simple, you can convert it to rem values ​​instead of px values, you can directly add the line -height property without units such as 1 or 1.5. You can enter values.

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

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