弹性列:如何在同一行上显示2个项目

时间:2018-12-13 13:54:18

标签: html css css3 flexbox

我目前正在尝试使用Flexbox在同一行上显示2个项目,在下面的示例中,我想在同一行上显示Item 1Item 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>

1 个答案:

答案 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>