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).
With Bootstrap v4, this can be accomplished just by adding
.justify-content-center
to the.row
<div>
https://getbootstrap.com/docs/4.0/utilities/flex/#justify-content
This is not my code, but it works perfectly (tested on Bootstrap 3) and I don't have to mess around with col-offset.
Demo:
If you put this on your row, all of the columns inside will be centered:
Try this
You can use other
col
as well likecol-md-2
, etc.Bootstrap 3 now has a built-in class for this
.center-block
If you are still using 2.X then just add this to your CSS.
We can achieve this by using the table layout mechanism:
The mechanism is:
The sample demo is here