My Second Solution to the Product Preview Card
Design comparison
Solution retrospective
I need a better way to use flex and grid to position a content at the center of all devices.
Community feedback
- @correlucasPosted about 2 years ago
👾Hello Ebi, congratulations for your new solution!
I'll say you how I build this solution using grid for the layout and how to align it with flex.
1.The best way to build this component with two columns is by using
GRID LAYOUT
since it is simpler to manage the columns and then create the media query for mobile. Here’s the steps to create it withgrid
create the main block to hold all the content (you can use<main>
to wrap), set itswidth
asmax-width: 900px
(it's the container size) anddisplay: grid
/grid-template-column: 1fr 1fr
(this means that your component will have two columns with 50% of the container width each thats 450px).Then to create the mobile version, all you need to do is to change the container flow vertically with
grid-template-column: 1fr
.2.To make this alignment, first of all put
min-height: 100vh
to thebody
to make the body display 100% of the viewport height (this makes the container align to the height size thats now 100% of the screen height) size anddisplay: flex
eflex-direction: column
to align the child element (the container) vertically using the body as reference.3.A best practice to have all the image inside the container scaling and respecting the size of the container, you need to add
max-width: 100%
and add alsoobject-fit: cover
to make the image auto-crop when resizing inside the column:img { display: block; object-fit: cover; max-width: 100%; }
🐸 Use this mini game to learn
FLEXBOX
and all its properties by positioning the frog and become aFLEXBOX MASTER
here's the link: https://flexboxfroggy.com/✌️ I hope this helps you and happy coding!
1
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