Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


  • Make each column as a table cell
  • Make that div as a Table with fixed layout
  • Wrap all columns in one div
Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


  • Make each column as a table cell
  • Make that div as a Table with fixed layout
  • Wrap all columns in one div
Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


.col-center-block {
    float: none;
    display: block;
    margin: 0 auto;
    /* margin-left: auto; margin-right: auto; */
}
<div class="container">
    <div class="row">
        <div class="center-block">row center-block</div>
    </div>
    <div class="row">
        <div class="col-md-6 brd">
            <div class="center-block">1 center-block</div>
        </div>
        <div class="col-md-6 brd">
            <div class="center-block">2 center-block</div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xs-2 col-center-block">row col-xs-2 col-center-block</div>
</div>
col-center-block

I got this working without adding float:none by making sure the div centre-block has the style="width : ?px" style applied to it. For example an image width. Works with 3.2.2

Now Bootstrap 3.1.1 is working with .center-block, and this helper class works with the column system.

Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


.col-center-block {
    float: none;
    display: block;
    margin: 0 auto;
    /* margin-left: auto; margin-right: auto; */
}
<div class="container">
    <div class="row">
        <div class="center-block">row center-block</div>
    </div>
    <div class="row">
        <div class="col-md-6 brd">
            <div class="center-block">1 center-block</div>
        </div>
        <div class="col-md-6 brd">
            <div class="center-block">2 center-block</div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xs-2 col-center-block">row col-xs-2 col-center-block</div>
</div>
col-center-block

I got this working without adding float:none by making sure the div centre-block has the style="width : ?px" style applied to it. For example an image width. Works with 3.2.2

Now Bootstrap 3.1.1 is working with .center-block, and this helper class works with the column system.

Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


colCalculator(5, 3, 'md')
>> ["col-md-4", "col-md-4", "col-md-4", "col-md-4 col-md-push-2", "col-md-4 col-md-push-2"]
function colCalculator(totalNumberOfElements, elementsPerRow, screenSize) {
    var arrayFill = function (size, content) {
        return Array.apply(null, Array(size)).map(String.prototype.valueOf, content);
    };

    var elementSize = parseInt(12 / elementsPerRow, 10);
    var normalClassName = 'col-' + screenSize + '-' + elementSize;
    var numberOfFittingElements = parseInt(totalNumberOfElements / elementsPerRow, 10) * elementsPerRow;
    var numberOfRemainingElements = totalNumberOfElements - numberOfFittingElements;
    var ret = arrayFill(numberOfFittingElements, normalClassName);
    var remainingSize = 12 - numberOfRemainingElements * elementSize;
    var remainingLeftSize = parseInt(remainingSize / 2, 10);
    return ret.concat(arrayFill(numberOfRemainingElements, normalClassName + ' col-' + screenSize + '-push-' + remainingLeftSize));
}

For those looking to center the column elements on the screen when you don't have the exact number to fill your grid, I have written a little piece of JavaScript to return the class names:

If you have 5 elements and you want to have 3 per row on a md screen, you do this:

Keep in mind, the second argument must be dividable by 12.

Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


.col-X-2
.col-X-4
.col-centered{
    float: none;
    margin: 0 auto;
}
<div class="row">
    <div class="col-lg-1 col-centered"></div>
</div>
col-X-10
col-X-6
col-X-8

@DiegoFernandoMurilloValenci .clearfix wouldn't work in this case because it doesn't remove the float property on the elements (which is needed to center using margin: 0 auto), it just makes a container wrap any floated elements inside

@cheshireoctopus In that case what I would do is set up a .col-md-9.col-centered and then add a nested row and divide it equally in three columns with col-md-4 like this

If you want to center more then one column in a single line, see this answer stackoverflow.com/questions/28683329/

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.

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

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.

Thanks for the informative answer. Curious as how you could apply this if you had multiple odd-numbered colums within a row. For example: three div's with class col-md-3. How would you center those three divs?

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.

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

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:

can't use this on a column, because no float:none;

i think that there is no float:none in the center-block rule because there is a clearfix independent rule that you can add to elements for clearing floats.

Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


.center-block {
    float: none !important
}
<div class="col-md-6 center-block">Example</div>

@GianfrancoP. P. Your right, but... that depends on the load order and i wanted to provide a foolproof method.

Bootstrap CSS (Ver 3.7 and lower) uses: margin: 0 auto;, but it gets overridden by the float property of the size container.

I'm using Bootstrap 3.3.7 and this is the only method working for me!

PS: After you add this class don't forget to set classes by the right order.

Simply add this to your custom CSS file, editing Bootstrap CSS files directly is not recommended and cancells your ability to use a cdn.

this works. but don't think !important is necessary

Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


.centered {
    background-color: teal;
    text-align: center;
}

But it works and I do not see a reason to down vote.

Perhaps in your isolated testing case. I would add .centered img { display: inline; } to your answer and then it would be worthy of an up vote instead.

This works. A hackish way probably, but it works nicely. It was tested for responsive (Y).

in bootstrap 3, the .text-center class does the same thing natively, as I learned above.

works only because the element has diplay inline. An img may be set as a block.

Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


colCalculator(5, 3, 'md')
>> ["col-md-4", "col-md-4", "col-md-4", "col-md-4 col-md-push-2", "col-md-4 col-md-push-2"]
function colCalculator(totalNumberOfElements, elementsPerRow, screenSize) {
    var arrayFill = function (size, content) {
        return Array.apply(null, Array(size)).map(String.prototype.valueOf, content);
    };

    var elementSize = parseInt(12 / elementsPerRow, 10);
    var normalClassName = 'col-' + screenSize + '-' + elementSize;
    var numberOfFittingElements = parseInt(totalNumberOfElements / elementsPerRow, 10) * elementsPerRow;
    var numberOfRemainingElements = totalNumberOfElements - numberOfFittingElements;
    var ret = arrayFill(numberOfFittingElements, normalClassName);
    var remainingSize = 12 - numberOfRemainingElements * elementSize;
    var remainingLeftSize = parseInt(remainingSize / 2, 10);
    return ret.concat(arrayFill(numberOfRemainingElements, normalClassName + ' col-' + screenSize + '-push-' + remainingLeftSize));
}

For those looking to center the column elements on the screen when you don't have the exact number to fill your grid, I have written a little piece of JavaScript to return the class names:

If you have 5 elements and you want to have 3 per row on a md screen, you do this:

Keep in mind, the second argument must be dividable by 12.

Note
Rectangle 27 0

css Center a column using Twitter Bootstrap 3?


<div class="container-fluid">
    <div class="row">
        <div class="col-lg-4 col-lg-offset-4">
            <img src="some.jpg">
        </div>
    </div>
</div>
Note