Eric Burel
Eric Burel

Reputation: 4944

How to use flex-grow?

In the doc and in the issues of Bootstrap v4 (here), I can't see any plan on supporting flex-grow, something with a syntax like this for example:

<div class="d-flex">
      <div  class="flex-grow">
        I use all the space left
      </div>
      <div>
        I am a small text on the right
      </div>
</div>

Here a sample of the layout I'd like to create:

image

The export button is always on the right, and the navigation button takes all the space left and therefore looks clean.

Is it possible to build such a layout already? Maybe it is not advised? Of course, there are workarounds using CSS, but I am looking for a clean solution, ideally using Bootstrap class names only to guarantee consistency.

Upvotes: 47

Views: 71587

Answers (3)

LTroya
LTroya

Reputation: 540

I created a class for this, I checked the Bootstrap docs and found nothing about it.

.flex-2 { flex-grow: 2 }

col-auto takes the remaining space but if you have more than one row, so space won't be distributed equally. That's why flex-grow does the trick.

Upvotes: 2

user4914734
user4914734

Reputation:

For anyone comming here from a google search (like me).

Since Bootstrap v 4.1 there are flex-grow and flex-shrink utilities, as the documentation says you can use them like this:

.flex-{grow|shrink}-0
.flex-{grow|shrink}-1
.flex-sm-{grow|shrink}-0
.flex-sm-{grow|shrink}-1

Here is a little example

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />

<div class="d-flex flex-row">
  <div class="d-flex flex-grow-1 text-white justify-content-center bg-danger">
    Grow 1
  </div>
  <div class="d-flex flex-grow-0 text-white justify-content-center bg-success">
    Grow 0
  </div>
  <div class="d-flex flex-grow-1 text-white justify-content-center bg-danger">
    Grow 1
  </div>
</div>

Upvotes: 47

Michael Coker
Michael Coker

Reputation: 53709

use .col for a simple flex-grow: 1;. It's described here https://v4-alpha.getbootstrap.com/layout/grid/#variable-width-content

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col">
      I use all the space left
    </div>
    <div class="col-sm col-sm-auto">
      I am a small text on the right
    </div>
  </div>
</div>

Upvotes: 50

Related Questions