Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox div text wrapping

Tags:

html

css

flexbox

I want to make text in my flexbox layout wrap.

Code: https://jsfiddle.net/u2oswnth/2/

.child {
  border: solid 1px;
  display: flex;
}
.left {
  width: 100px;
}
.container {
  display: flex;
  flex-wrap: wrap;
}
<div class="container">
  <div class="child left">
    Left!
  </div>
  <div class="child right">
    I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!!
  </div>
</div>

What I have: When exceeding frame width, .right div falls to next row.

What I want: When exceeding frame width, words in .right div start wrapping, two columns stay side-by-side, forming single row.

like image 886
StragaSevera Avatar asked Dec 15 '15 07:12

StragaSevera


2 Answers

Change flex-wrap: wrap; to flex-wrap: nowrap;. By using flex-wrap: wrap; you are telling the .right div to wrap onto a new line if it runs out of space. As you only want the text itself to wrap you need to use flex-wrap: nowrap; to keep .right on the same line. The text will automatically wrap when there is not enough space.

  • Change flex-wrap: wrap; to flex-wrap: nowrap; on .container

.child {
  border: solid 1px;
  display: flex;
}
.left {
  width: 100px;
}
.container {
  display: flex;
  flex-wrap: nowrap;
}
<div class="container">
  <div class="child left">
    Left!
  </div>
  <div class="child right">
    I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!!
  </div>
</div>
like image 77
Hidden Hobbes Avatar answered Oct 12 '22 11:10

Hidden Hobbes


NOTE: There is no need to apply "display: flex" on child class and flex-wrap: nowrap; on container class because bydefault it is nowrap. Below is updated answer and WORKS!!.

.child {
  border: solid 1px;
}
.left {
  width: 100px;
}
.container {
  display: flex;
} 

<div class="container">
  <div class="child left">
    Left!
  </div>
  <div class="child right">
    I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!! I'm riiiight!!!
  </div>
</div>
like image 36
solanki... Avatar answered Oct 12 '22 13:10

solanki...