Center a Column Using Twitter Bootstrap 3

Center a column using Twitter Bootstrap 3

There are two approaches to centering a column <div> in Bootstrap 3:

Approach 1 (offsets):

The first approach uses Bootstrap's own offset classes so it requires no change in markup and no extra CSS. The key is to set an offset equal to half of the remaining size of the row. So for example, a column of size 2 would be centered by adding an offset of 5, that's (12-2)/2.

In markup this would look like:

<div class="row">
<div class="col-md-2 col-md-offset-5"></div>
</div>

Now, there's an obvious drawback for this method. It only works for even column sizes, so only .col-X-2, .col-X-4, col-X-6, col-X-8, and col-X-10 are supported.


Approach 2 (the old margin:auto)

You can center any column size by using the proven margin: 0 auto; technique. You just need to take care of the floating that is added by Bootstrap's grid system. I recommend defining a custom CSS class like the following:

.col-centered{
float: none;
margin: 0 auto;
}

Now you can add it to any column size at any screen size, and it will work seamlessly with Bootstrap's responsive layout:

<div class="row">
<div class="col-lg-1 col-centered"></div>
</div>

Note: With both techniques you could skip the .row element and have the column centered inside a .container, but you would notice a minimal difference in the actual column size because of the padding in the container class.


Update:

Since v3.0.1 Bootstrap has a built-in class named center-block that uses margin: 0 auto, but is missing float:none, you can add that to your CSS to make it work with the grid system.

Center a 9-column layout using twitter bootstrap 3

You can use nesting and col-*-offset-* to center odd numbers of columns (where you have 3 in a row). The case of the row with 2 columns can be simply centered using offsets (no nesting required). Use text-center to center the content inside the columns.

<div class="container-fluid">
<div class="row">
<div class="col-md-11 col-md-offset-1">
<div class="row">
<div class="col-md-3 col-md-offset-1 text-center"></div>
<div class="col-md-3 text-center"></div>
<div class="col-md-3 text-center"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-2 col-md-offset-4 text-center"></div>
<div class="col-md-2 text-center"></div>
</div>
</div>

Demo: http://bootply.com/HKy0mPMXv5

how can I center these 3 columns using bootstrap

The issue with your layout is that in order to center your 3 columns, you would have to offset the left column by 1.5 (half of the leftover columns):

// 12 Column Layout, 3 columns of col-md-3, offset of 1.5 needed (which isn't a class) 
12 - (3 + 3 + 3) = 3 / 2 = 1.5

Since that won't work (as col-md-offset-1.5 isn't a class) I think you have 2 options here:

Option 1: Change your col-md-3 to col-md-4 and put it in a container:

<div class="container">
<div class="col-md-4 box">
<div>
<h4>box 1</h4>
<p>some text dfgdfgdfgdfg</p>
</div>
</div>
<div class="col-md-4 box">
<div>
<h4>box 2</h4>
<p>some text dfgdfgdfgdfg</p>
</div>
</div>
<div class="col-md-4 box">
<div>
<h4>box 3</h4>
<p>some text dfgdfgdfgdfg</p>
</div>
</div>
</div>

Bootply Example 1

Option 2: Change your col-md-3 to col-md-2 and col-md-offset-1 to col-md-offset-3 and put it in container-fluid:

<div class="container-fluid">
<div class="col-md-2 col-md-offset-3 box">
<div>
<h4>box 1</h4>
<p>some text dfgdfgdfgdfg</p>
</div>
</div>
<div class="col-md-2 box">
<div>
<h4>box 2</h4>
<p>some text dfgdfgdfgdfg</p>
</div>
</div>
<div class="col-md-2 box">
<div>
<h4>box 3</h4>
<p>some text dfgdfgdfgdfg</p>
</div>
</div>
</div>

Bootply Example 2

Both methods change the layout so that the full 12 columns are taken up (or offset correctly), but one gives you a bit more space to work with.

For more info on the Bootstrap Grid see the Documentation

Center a column using Twitter Bootstrap 3

There are two approaches to centering a column <div> in Bootstrap 3:

Approach 1 (offsets):

The first approach uses Bootstrap's own offset classes so it requires no change in markup and no extra CSS. The key is to set an offset equal to half of the remaining size of the row. So for example, a column of size 2 would be centered by adding an offset of 5, that's (12-2)/2.

In markup this would look like:

<div class="row">
<div class="col-md-2 col-md-offset-5"></div>
</div>

Now, there's an obvious drawback for this method. It only works for even column sizes, so only .col-X-2, .col-X-4, col-X-6, col-X-8, and col-X-10 are supported.


Approach 2 (the old margin:auto)

You can center any column size by using the proven margin: 0 auto; technique. You just need to take care of the floating that is added by Bootstrap's grid system. I recommend defining a custom CSS class like the following:

.col-centered{
float: none;
margin: 0 auto;
}

Now you can add it to any column size at any screen size, and it will work seamlessly with Bootstrap's responsive layout:

<div class="row">
<div class="col-lg-1 col-centered"></div>
</div>

Note: With both techniques you could skip the .row element and have the column centered inside a .container, but you would notice a minimal difference in the actual column size because of the padding in the container class.


Update:

Since v3.0.1 Bootstrap has a built-in class named center-block that uses margin: 0 auto, but is missing float:none, you can add that to your CSS to make it work with the grid system.

How to center content in a Bootstrap column?

Use:

<!-- unsupported by HTML5 -->
<div class="col-xs-1" align="center">

instead of

<div class="col-xs-1 center-block">

You can also use bootstrap 3 css:

<!-- recommended method -->
<div class="col-xs-1 text-center">

Bootstrap 4 now has flex classes that will center the content:

<div class="d-flex justify-content-center">
<div>some content</div>
</div>

Note that by default it will be x-axis unless flex-direction is column

Twitter Bootstrap 3 conditional .text-center

Only way I can think of is have a custom class like follows. Seems like it could be refactored somehow, but it's what I can think of now:

<div class="row">
<div class="col-xs-12 col-sm-4 customTextLeft text-uppercase">Left Col</div>
<div class="col-xs-12 col-sm-8 customTextRight">Right Col</div>
</div>
/*CSS*/
/*screen-xs*/
@media (max-width: 768px) {
.customTextLeft{text-align:center;}
}
/*screen-sm*/
@media (min-width: 768px) and (max-width: 992px) {
.customTextLeft{text-align:left;}
}
/*screen-md*/
@media (min-width: 992px) and (max-width: 1200px) {
.customTextLeft{text-align:left;}
}
/*screen-lg corresponds with col-lg*/
@media (min-width: 1200px) {
.customTextLeft{text-align:left}
}

/*screen-xs*/
@media (max-width: 768px) {
.customTextRight{text-align:center;}
}
/*screen-sm*/
@media (min-width: 768px) and (max-width: 992px) {
.customTextRight{text-align:right;}
}
/*screen-md*/
@media (min-width: 992px) and (max-width: 1200px) {
.customTextRight{text-align:right;}
}
/*screen-lg corresponds with col-lg*/
@media (min-width: 1200px) {
.customTextRight{text-align:right}
}

Twitter Bootstrap 3: How to center a block

It's new in the Bootstrap 3.0.1 release, so make sure you have the latest (10/29)...

Demo: http://bootply.com/91632

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/><div class="row">    <div class="center-block" style="width:200px;background-color:#ccc;">...</div></div>

How to center a two column layout using Twitter Bootstrap 3?

Solved this problem by overriding the padding-left and padding-right of the .container class in bootstrap 3.

.container {
padding-right: 120px;
padding-left: 120px;
}

It might be better to do the above with percentages so that it scales. I was thrown off by the fact that my sidebar was not extending fully to the end of the container and as a result though it was not being centered.

Adding the above override did it for me.

Many thanks to @trevor for his test bed.

How do you get centered content using Twitter Bootstrap?

This is for Text Centering (which is what the question was about)

For other types of content, see Flavien's answer.

Update: Bootstrap 2.3.0+ Answer

The original answer was for an early version of bootstrap. As of bootstrap 2.3.0, you can simply give the div the class .text-center.


Original Answer (pre 2.3.0)

You need to define one of the two classes, row or span12 with a text-align: center. See http://jsfiddle.net/xKSUH/ or http://jsfiddle.net/xKSUH/1/



Related Topics



Leave a reply



Submit