在flex项目中包装文本

时间:2016-03-29 17:18:43

标签: html css flexbox

我有两个div容器。第一个容器固定300px。第二个div应该填充外部div。为了解决这个问题,我使用了flexboxes。我现在的问题是,第二个div不包含内容。我该如何解决这个问题?

Here is a JSfiddle



*{
  margin: 0px;
  padding: 0px;
}

.content{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 300px;
  padding: 10px;
  border: 1px solid black;
}

.left{
  width: 200px;
  float: left;
  background-color: red;
  height: 20px;
  margin-right: 10px;
}

.right{
  flex-grow: 1;
  float: right;
  background-color: green;
  height: 20px;
}

.clearBoth{
  clear: both;
}

<div class="content">
  <div class="left">
    
  </div>
  <div class="right">
    Here is Some Text
  </div>
  <div class="clearBoth"></div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

首先,您不能在Flex容器中使用float,原因是 float属性不适用于弹性级别框

您还应该从height项中移除.right,然后使用flex: 1

* {
  margin: 0px;
  padding: 0px;
}
.content {
  display: flex;
  width: 300px;
  padding: 10px;
  border: 1px solid black;
}
.left {
  width: 200px;
  background-color: red;
  height: 10px;
  margin-right: 10px;
}
.right {
  flex: 1;
  background-color: blue;
  color: white;
}
<div class="content">
  <div class="left"></div>
  <div class="right">Here is Some Text</div>
</div>

答案 1 :(得分:2)

float不会影响flex项目...我认为这就是你所追求的目标。

*{
  margin: 0px;
  padding: 0px;
}

.content{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 300px;
  padding: 10px;
  border: 1px solid black;
}

.left{
  flex: 0 0 200px;
  background-color: red;
  margin-right: 10px;
}

.right{
  flex: 1;
  background-color: green;
}

.clearBoth{
  clear: both;
}
<div class="content">
  <div class="left">
    
  </div>
  <div class="right">
    Here is Some Text
  </div>
  <div class="clearBoth"></div>
</div>