How do I center a div of one column size within the container (12 columns) in Twitter Bootstrap 3.
Please see the starter fiddle.
<body class="container">
<div class="col-lg-1 col-offset-6 centered">
<img data-src="holder.js/100x100" alt="" />
</div>
</body>
So, I want a div
, with a class centered
to be centered within the container. I may use a row if there are multiple divs, but for now I just want a div with size of one column centered within the container (12 columns).
I am also not sure the above approach is good enough as the intention is not to offset the div
by half. I do not need free spaces outside the div
and the contents of the div
shrink in proportion. I want to empty space outside the div to be evenly distributed (shrink till the container width == one column).
Try this code.
Here i have used col-lg-1, and the offset should be 10 for properly centered the div on large devices, if you need it to center on mediam to large device then just change the lg to md and so on, let me know if any issue.
To center the col- we need to use the below code. cols are floater elements besides margin auto. We will also set it to float none,
To center the above col-lg-1 with class of centered, we will write:
To center the content inside the div, use
text-align:center
,If you want to center it only on the desktop and larger screen, not on mobile, then use the following media query.
And to center the div only on mobile version, use the below code.
Simply add this to your custom CSS file, editing Bootstrap CSS files directly is not recommended and cancells your ability to use a cdn.
Why?
Bootstrap CSS (Ver 3.7 and lower) uses: margin: 0 auto;, but it gets overridden by the float property of the size container.
PS: After you add this class don't forget to set classes by the right order.
To be more precise Bootstrap's grid system contains 12 columns and to center any content let’s say, for instance, the content takes up one column. One will need to occupy two columns of Bootstrap's grid and place the content on half of the two occupied columns.
'col-xs-offset-5' is telling the grid system where to start placing the content.
'col-xs-2' is telling the grid system how many of the left over columns should the content occupy.
'centered' will be a defined class that will center the content.
Here is how this example looks like in Bootstrap's grid system.
Columns:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
.......offset....... .data. .......not used........
Now Bootstrap 3.1.1 is working with
.center-block
, and this helper class works with the column system.Bootstrap 3 Helper Class Center.
Please check this jsfiddle DEMO:
Row column center using
col-center-block
helper class.