htmlcssflexbox

Wrap Text inside flex item


I have two div containers. The first container got a fixed with of 300px. The second div should fill the outer div. To solve this problem, I used flexboxes. My problem is now, that the second div don't wrap the content. How can I fix this problem?

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>


Solution

  • First you can't use float inside flex container and the reason is that float property does not apply to flex-level boxes.

    You should also remove height from .right item and use flex: 1 instead.

    * {
      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>