Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flexbox grow vertical

Tags:

html

css

flexbox

So I am new to flexbox and I have been playing around with it for some time. I have a requirement that the middle layout needs to grow vertically. Here's what I have:

.main-container {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    font-size: 16px;
    background: #F7F8FC;
}

.sub-container {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    white-space: nowrap;
}
<div class="main-container">
  <div class="flex-row-item left">
    <button>
      <span>Email</span>
    </button>
  </div>
  <div class="flex-row-item center">
    <div class="sub-container">
      <button>
        <span>Previous</span>
      </button>
      <div class="sub-text">
        <span>Item Name: THIS IS SOME TEXT THAT IS really really really long</span>
      </div>
      <button>
        <span>Next</span>
      </button>
    </div>
  </div>
  <div class="flex-row-item right">
    <button>
      <span>Help</span>
    </button>
  </div>
</div>

So when the text in "sub-text" starts to get longer and longer, current styling makes the flexbox breaks into a new line along with the "Previous", "Next" and "Help" buttons. I do not want that to happen. Instead how can I just grow the "sub-text" vertically with word wrap while expanding the main container height? For example see this : Example

like image 963
Pahan Avatar asked May 25 '26 23:05

Pahan


1 Answers

use white-space: pre-line

.sub-text{
    white-space: pre-line;
}

and there is no need for flex-wrap: wrap;

working example:

.main-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 16px;
    background: #F7F8FC;
}

.sub-container {
    display: flex;
    align-items: center;
    white-space: nowrap;
}

.sub-text{
    white-space: pre-line;
}
<div class="main-container">
  <div class="flex-row-item left">
    <button>
      <span>Email</span>
    </button>
  </div>
  <div class="flex-row-item center">
    <div class="sub-container">
      <button>
        <span>Previous</span>
      </button>
      <div class="sub-text">
        <span>Item Name: THIS IS SOME TEXT THAT IS really really realddddly londdddddddddddddddddddddddddg</span>
      </div>
      <button>
        <span>Next</span>
      </button>
    </div>
  </div>
  <div class="flex-row-item right">
    <button>
      <span>Help</span>
    </button>
  </div>
</div>

Learn more

like image 156
Yasin Br Avatar answered May 28 '26 13:05

Yasin Br