Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to center div on bootstrap 3

I'm unable to center a lot of div since I upgrade my bootstrap from 2.1 to 3.0

For example with this code:

<div id="center" class="container">
    <div class="row">
        <div class="btn-toolbar">
            <div class="btn-group">
                <a class="btn btn-default" href="#">test</a>
            </div>
        </div>

        <br />

        <p>Am I centered ?</p>
        <a class="btn btn-default" href="#">Back</a>
    </div>

</div>

I had this rule:

#center {
    margin: 0 auto;
}

But the result is: enter image description here

Or another example, how to center this:

<div id="center" class="container">
    <div class="row">
        <li class="col-md-5">
            <ul class="list-unstyled">
                <li><i class="icon-user"></i> aaaaaaaaa</li>
                <li><i class="icon-envelope"></i> bbbbbbbbbb</li>
                <li><i class="icon-envelopebug"></i> cccccccccccc</li>
            </ul>
        </li>
        <li class="col-md-5">
            <ul class="list-unstyled">
                <li><i class="icon-user"></i> aaaaaaaaa</li>
                <li><i class="icon-envelope"></i> bbbbbbbbbb</li>
                <li><i class="icon-envelopebug"></i> cccccccccccc</li>
            </ul>
        </li>
    </div>
</div>

enter image description here

Thank you for your help

like image 750
ZazOufUmI Avatar asked Sep 26 '13 23:09

ZazOufUmI


People also ask

How do I center a div in Bootstrap 3?

Using Twitter Bootstrap's center-block helper class, which is available in Bootstrap 3.2, you may center a div. It is evident from the code above that this class ensures that the block associated is displayed as a block-level element and it must have margins set as required to place it to the center.

How do I center a div in bootstrap?

To center div both vertically and horizontally use flexbox utilities. See the examples. Add d-flex align-items-center justify-content-center classes to the parent element to center its content vertically and horizontally.

How do I force a div to center?

Simply add text-align center to parent div and set the child div display to inline-block. This will force our div to behave like inline element and therefore subjected text-align center.

How do I fix a div in the center of the screen?

You basically need to set top and left to 50% to center the left-top corner of the div. You also need to set the margin-top and margin-left to the negative half of the div's height and width to shift the center towards the middle of the div.


2 Answers

In order to center a block level element using margin: 0 auto; it must also have a width that is smaller than its containing block (for the auto value to make sense) - because #container is spanning the width of its parent (the <body>) there is simply no margin to distribute.

An alernative approach to margin: 0 auto; would be to set .btn-toolbar to inline-block and then centering it by adding text-align: center; to its containing block. You can apply the same concept to the second example:

http://fiddle.jshell.net/52VtD/94/

like image 180
Adrift Avatar answered Sep 19 '22 18:09

Adrift


In this instance, margin:0 auto doesn't work because the width of the element is 100%. If you want it to work, you would have to set a width on the element:

.btn-toolbar {
    width: 50px;
    margin: 0px auto;
}

If you want to center the text and the button, you could add the class text-center to the parent element, in this case: .row. The styling of this class is simply text-align: center.

<div class="row text-center">
  ..
</div>

EXAMPLE HERE

As @Adrift points out, it would be much more efficient to center the element by making it inline-block, as you can use text-align:center as opposed to margin:0 auto and avoid having to set a fixed width on the element. This will ensure that the element is centered regardless of its width. (example here) - don't forget you can just add the class text-center to the parent for centering.

It's also worth noting that inline/inline-block elements respect white-space in the markup, and thus generate space if present. If you want to remove this space, see this answer.

like image 24
Josh Crozier Avatar answered Sep 23 '22 18:09

Josh Crozier