odnt
odnt

Reputation: 99

How to vertically justify in Bootstrap 4 columns

I have this structure:

<div class=”row”>
    <div class=”col”>
        <div class=”float-to-top”></div>
        <div class=”float-to-bottom></div>
    </div>

    <div class=”col”>
        <div class=”float-to-top”></div>
        <div class=”float-to-bottom></div>
    </div>
</div>

The task is to align the divs of the same class with each other. Being Bootstrap 4, both columns are the same height, for free; and I can align the tops or the bottoms. But how to "justify" vertically?

Oh, and using media queries is not allowed; only the Bootstrap facilities.

Thanks for any help.

Upvotes: 1

Views: 3307

Answers (1)

Obsidian Age
Obsidian Age

Reputation: 42304

It sounds like you want to swap rows and columns. So that your two .col classes sit next to each other, and your .float-to-top are at the top (with .float-to-bottom at the bottom).

If that's the case you're looking for the following on .col:

  • display: flex to make use of flexbox
  • flex-direction: column to swap the layout
  • justify-content: space-between to space the elements out (top and bottom)
  • a height to add the space in between the elements

And possibly:

  • align-items: center for horizontal centering

This can be seen in the following:

.col {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 180px;
  align-items: center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="row">
  <div class="col">
    <div class="float-to-top">Top 1</div>
    <div class="float-to-bottom">Bottom 1</div>
  </div>
  <div class="col">
    <div class="float-to-top">Top 2</div>
    <div class="float-to-bottom">Bottom 2</div>
  </div>
</div>

Upvotes: 2

Related Questions