Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center three col-md-3 columns in Bootstrap 4

Is there a way to have three col-md-3 columns and center them. Offset doesn't work, because I'd have to offset the first column for a column and a half. So is there another way to do this?

Here's the outline of the code:

.col-md-3 {
  background-color: #e2e2e2;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class="col-md-3">First column</div>
    <div class="col-md-3">Second column</div>
    <div class="col-md-3">Third column</div>
  </div>
</div>

The answers I managed to find on SO were all related to Bootstrap 3 and lower. And didn't work with Bootstrap 4. Can someone take a look and let me know?

like image 688
Retros Avatar asked Jan 19 '18 10:01

Retros


1 Answers

The Flexbox utility classes are your friend.
You can use justify-content-center on .row in this case.

.row {
    background-color: lavender;
}

.col-md-3 {
    background-color: gray;
}
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-3">First column</div>
        <div class="col-md-3">Second column</div>
        <div class="col-md-3">Third column</div>
    </div>
</div>


<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
like image 144
dferenc Avatar answered Sep 23 '22 15:09

dferenc