Div 不像我想的那样浮动
Divs don't float how I would
我在单个 div 中有 5 个 div,我会让它在左侧浮动 2 个,在右侧浮动 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>
您可以将 div 包裹在 Columns 中,并将这两个浮动。或者你可以使用 flexbox
,Nenad Vracar 对此有很好的回答。
这是一个包含两个列包装 div
元素的示例:
.container {
height: 400px;
border: 1px solid black;
}
.col {
width: 50%;
float: left;
}
.first {
height: 300px;
background-color: red;
}
.second {
height: 100px;
background-color: blue;
}
.third {
height: 100px;
background-color: green;
}
.fourth {
height: 200px;
background-color: yellow;
}
.fifth {
height: 100px;
background-color: aquamarine;
}
<div class="container">
<div class="col">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
</div>
<div class="col">
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>
</div>
我在单个 div 中有 5 个 div,我会让它在左侧浮动 2 个,在右侧浮动 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>
您可以将 div 包裹在 Columns 中,并将这两个浮动。或者你可以使用 flexbox
,Nenad Vracar 对此有很好的回答。
这是一个包含两个列包装 div
元素的示例:
.container {
height: 400px;
border: 1px solid black;
}
.col {
width: 50%;
float: left;
}
.first {
height: 300px;
background-color: red;
}
.second {
height: 100px;
background-color: blue;
}
.third {
height: 100px;
background-color: green;
}
.fourth {
height: 200px;
background-color: yellow;
}
.fifth {
height: 100px;
background-color: aquamarine;
}
<div class="container">
<div class="col">
<div class="first"> FIRST </div>
<div class="second"> SECOND </div>
</div>
<div class="col">
<div class="third"> THIRD </div>
<div class="fourth"> FOURTH</div>
<div class="fifth"> FIFTH </div>
</div>
</div>