005-product-preview-card-component - HTML, CSS, Flexbox
Design comparison
Solution retrospective
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
.
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("..."); }
Community feedback
- @devkhrmnturkPosted about 2 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 torem
values instead ofpx
values, you can directly add theline -height
property without units such as1
or1.5
. You can enter values.0
Please log in to post a comment
Log in with GitHubJoin 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