如何每行创建 4 个弹性盒容器

问题描述 投票:0回答:1

我只是想将我拥有的 8 个弹性框项目(名称:

products-box
)移动到每行 4 个。这是我到目前为止所拥有的:

*{
    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>

我正在尝试让标记为

products-box
的 Flexbox 每行 4 个,即使不是。

html css
1个回答
0
投票

尝试稍微调整你的CSS中的.products-box:

.products-box {
    flex: 0 0 25%; /* Set flex basis to 25% */
    max-width: 25%; /* Restrict maximum    width to 25% */
    height: 200px;
    background-color: lightpink;
    color: white;
    font-size: 20px;
    text-align: center;
    line-height: 200px;
}

希望有帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.