Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap Text inside flex item

Tags:

html

css

flexbox

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>
like image 360
MyNewName Avatar asked Mar 29 '16 17:03

MyNewName


2 Answers

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>
like image 69
Nenad Vracar Avatar answered Oct 24 '22 19:10

Nenad Vracar


float does not affect flex-items...I think this is what you are after.

*{
  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>
like image 28
Paulie_D Avatar answered Oct 24 '22 18:10

Paulie_D