I am simply looking to move the 8 flex box items I have (name: products-box
) to 4 per row. Here is what I have so far:
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
body{
min-height: 100vh;
}
header{
height: 80px;
display: flex;
background-color: lightblue;
}
.products-container{
width: 100%;
height: auto;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
}
.products-box{
width: 200px;
height: 200px;
background-color: lightpink;
color: white;
font-size: 20px;
text-align: center;
line-height: 200px;
}
.main-container{
display: flex;
min-height: 100vh;
}
.nav-sidebar{
min-width: 150px;
background-color: lightgray;
}
.inner-text{
font-weight: 600;
font-size: 24px;
margin: auto;
}
<header>
<span class="inner-text">Header</span>
</header>
<div class="main-container">
<aside class="nav-sidebar" id="nav-container">
<span class="inner-text">Sidebar</span>
</aside>
<div class="products-container">
<div class="products-box">
Item1
</div>
<div class="products-box">
Item2
</div>
<div class="products-box">
Item3
</div>
<div class="products-box">
Item4
</div>
<div class="products-box">
Item5
</div>
<div class="products-box">
Item6
</div>
<div class="products-box">
Item7
</div>
<div class="products-box">
Item8
</div>
</div>
</div>
I am trying to get the flexbox labeled products-box
to be 4 per row, and be even if not.
3
Answers
Try tweaking your .products-box in your css a little bit:
Hope that helps 🙂
Since a parent container/row has a width of 100%.
To have 4 product-boxes per row, each
product-box
should occupy 25% of the its parent element.Check it out below code:
For displaying products, I prefer a grid. The snippet below demonstrates a grid with a fixed number of columns.
The potential problem with this is that the size of each product box varies greatly depending on the screen width. If you want to keep the size of the product boxes more consistent and instead vary the number of products in each row, you could use an auto-fill.