Millhorn
Millhorn

Reputation: 3166

How to vertically align item in Bootstrap 4?

I'm trying to vertically align text inside of a column. If I use my-auto in the parent (column), then it vertically aligns the text, but it pulls in everything to the center and I need to maintain the background. The background color is currently on the column.

When I remove my-auto, the full orange background returns, but the text is at the top.

I've tried creating an inner row>column structure to account for it, but it doesn't work. Any thoughts?

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid">
  <div class="row no-gutters">
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-4 col-xl-4 my-auto" style="background-color: #f04e23;">
      <h1 class="display-4 text-center">2018 Speakers</h1>
    </div> 
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
  </div>
</div>

Upvotes: 0

Views: 82

Answers (1)

daGUY
daGUY

Reputation: 28743

Instead of using margins, set the column's display to flex by adding the .d-flex class, then use the .align-items-center class to center the text vertically. You could also then add .justify-content-center to center it horizontally:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid">
  <div class="row no-gutters">
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-4 col-xl-4 d-flex align-items-center justify-content-center" style="background-color: #f04e23;">
      <h1 class="display-4 text-center">2018 Speakers</h1>
    </div> 
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
    <div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 col-xl-2">
      <div class="card">
        <img class="img-fluid" src="https://picsum.photos/400/600" alt="" />
      </div>
    </div>
  </div>
</div>

Upvotes: 2

Related Questions