我目前正在尝试使用Flexbox在同一行上显示2个项目,在下面的示例中,我想在同一行上显示Item 1
和Item 2
我已经尝试添加flex: 1 0 50%;
,但是它不起作用
.container {
flex: 1;
display: flex;
border: 1px solid #000;
background: #FFF;
flex-direction: column;
}
.item {
padding: 14px;
margin: 12px;
background: #ED8896;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
答案 0 :(得分:2)
您可以在下面找到解决方案:
.container {
display: flex;
border: 1px solid #000;
background: #FFF;
flex-direction: row; /* change this to row instead of 'column' */
flex-wrap:wrap; /* added flex-wrap */
}
.item {
padding: 14px;
box-sizing: border-box; /* I added */
margin: 12px;
background: #ED8896;
width:100%; /* I added */
}
.item:nth-child(1){
width:calc(50% - 24px);
}
.item:nth-child(2){
width:calc(50% - 24px);
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>