gpt4 book ai didi

html - Div 不像我想的那样 float

转载 作者:行者123 更新时间:2023-11-28 10:29:15 26 4
gpt4 key购买 nike

我在一个 div 中有 5 个 div,我会让它在左边 float 2 个,在右边 float 3 个:

.container {
height: 400px;
border: 1px solid black;
}
.first, .second, .third, .fourth, .fifth {
width: 50%;
}

.first {
height: 300px;
background-color: red;
float: left;
}

.second {
height: 100px;
background-color: blue;
float: left;
}

.third {
height: 100px;
background-color: green;
float: right;
}

.fourth {
height: 200px;
background-color: yellow;
float: right;
}

.fifth {
height: 100px;
background-color: aquamarine;
float: right;
}
<div class="container">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>

我希望它们被放置在:

FIRST and SECOND at left
THIRD, FORTH and FIFTH at right.
Instead, they are placed like:
FIRST and FIFTH at left
SECOND, THIRD and FOURTH at right.

你知道怎么解决吗?这是我的代码:https://jsfiddle.net/82bkdbpn/

最佳答案

因为您已经在 container 元素上定义了固定高度,您可以使用 Flexbox 来执行此操作并定义 flex-direction: column

.container {
height: 400px;
border: 1px solid black;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.first, .second, .third, .fourth, .fifth {
width: 50%;
height: 100px;
}
.first {
height: 300px;
background-color: red;
}
.fourth {
height: 200px;
background-color: yellow;
}
.second {background-color: blue;}
.third {background-color: green;}
.fifth {background-color: aquamarine;}
<div class="container">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>

关于html - Div 不像我想的那样 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46885426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com