Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center multiple bootstrap columns when less than 12 col

<div class="row">
    <div ng-repeat="tile in tiles" class="col-md-3"></div>
</div>

How can I make sure that these bootstrap columns are always centered, even if there is only 2 of them?

like image 410
parliament Avatar asked May 19 '15 02:05

parliament


People also ask

How do I center all columns in Bootstrap?

Use d-flex justify-content-center on your column div. This will center everything inside that column. If you have text and image inside the column, you need to use d-flex justify-content-center and text-center . <div class="col text-center"> worked for me.

How do I center two columns in Bootstrap?

Horizontal Alignment To horizontally align columns, we can use the justify-content classes. justify-content-start left align the columns. justify-content-center center aligns the columns. justify-content-end right align the columns.

Is Bootstrap always 12 columns?

Bootstrap uses a 12-column grid system that can update itself responsively based on screen size. There are only 38 highly composite numbers below the one million threshold, including 120, 2520, 5040, 55440 and 720720.

Can we use more than 12 columns in Bootstrap?

The Bootstrap grid has only 12 columns, so you should never have more than 12 columns in a row. You should have only 3 col-md-4 in each .


2 Answers

Add row-center class to the row and col-center to col-md-3 in this way:

<div class="row row-center">
  <div ng-repeat="tile in tiles" class="col-md-3 col-center"></div>
</div>

.row-center {
  text-align:center;
}

.col-center {
  display:inline-block;
  float:none;
}

Bootply Demo

like image 128
m4n0 Avatar answered Sep 29 '22 11:09

m4n0


Starting from bootstrap 4

this feature added!

you can use this class: justify-content-center

so your code should looks like:

<div class="container">
  <div class="row justify-content-center">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
</div>

Check bootstrap documentation: https://getbootstrap.com/docs/4.0/layout/grid/#horizontal-alignment

like image 43
Bassem Shahin Avatar answered Sep 29 '22 12:09

Bassem Shahin