How to center a div with Bootstrap2?

332,924

Solution 1

besides shrinking the div itself to the size you want, by reducing span size like so... class="span6 offset3", class="span4 offset4", etc... something as simple as style="text-align: center" on the div could have the effect you're looking for

you can't use span7 with any set offset and get the span centered on the page (Because total spans = 12)

Solution 2

Bootstrap's spans are floated to the left. All it takes to center them is override this behavior. I do this by adding this to my stylesheet:

.center {
     float: none;
     margin-left: auto;
     margin-right: auto;
}

If you have this class defined, just add it to the span and you're good to go.

<div class="span7 center"> box </div>

Note that this custom center class must be defined after the bootstrap css. You could use !important but that isn't recommended.

Solution 3

Bootstrap3 has the .center-block class that you can use. It is defined as

.center-block {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

Documentation here.

Solution 4

If you want to go full-bootstrap (and not the auto left/right way) you need a pattern that will fit within 12 columns e.g. 2 blanks, 8 content, 2 blanks. That's what this setup will do. It only covers the -md- variants, I tend to snap it to full size for small by adding col-xs-12

<div class="container">
  <div class="col-md-8 col-md-offset-2">
     box
  </div>
</div>

Solution 5

Sounds like you just wanted to center align a single container. The bootstrap framework might be overcomplicating that one example, you could have just had a standalone div with your own styling, something like:

<div class="login-container">
  <!-- Your Login Form -->
</div>

and style:

.login-container {
  margin: 0 auto;
  width: 400px; /* Whatever exact width you are looking for (not bound by preset bootstrap widths) */
}

That should work fine if you are nested somewhere within a bootstrap .container div.

Share:
332,924

Related videos on Youtube

Alex
Author by

Alex

I'm still learning so I'm only here to ask questions :P

Updated on April 29, 2020

Comments

  • Alex
    Alex about 4 years

    http://twitter.github.com/bootstrap/scaffolding.html

    I tried like all combinations:

    <div class="row">
      <div class="span7 offset5"> box </div>
    </div>
    

    or

    <div class="container">
      <div class="row">
        <div class="span7 offset5"> box </div>
      </div>  
    </div>
    

    changed span and offset numbers...

    But I cant get a simple box perfectly centered on a page :(

    I just want a 6-column-wide box centered...


    edit:

    did it with

    <div class="container">
      <div class="row" id="login-container">
        <div class="span8 offset2">
           box
        </div>
      </div>
    </div>
    

    But the box is too wide, is there any way I can do it with span7 ?

    span7 offset2 gives extra padding to the left span7 offset3 extra padding to the right...

    • Andres Ilich
      Andres Ilich over 12 years
      Are you looking to center a box horizontally on the page? Because the container class already does that, thought you might be looking to center a smaller box. Or are you looking to center a box both horizontally and vertically on the page?
    • pythonian29033
      pythonian29033 about 11 years
      a box thing you say? . . . .here in developer world we call that a div
    • JGallardo
      JGallardo over 8 years
      What version of Bootstrap were you using? it should be reflected in the title as people looking this up will end up here and see outdated code.
  • BigSauce
    BigSauce over 11 years
    YES! Thank you, Zuhaib! Worked perfectly. This answer should be voted as the correct answer for this question.
  • BigSauce
    BigSauce over 11 years
    Consider Zuhaib Ali's answer. text-align: center doesn't work for centering elements within a <div>. The offset* class isn't truly centering components. It is merely creating the illusion of centering. You can verify this by resizing the window and seeing that the components do not remain centered. When you apply Zuhaib Ali's method, the components stay centered.
  • SIFE
    SIFE about 11 years
    @ZuhaibAli Adding float: none; fix my problem.
  • Admin
    Admin about 11 years
    I'm using Rails, so I just want to point out that in application.css, add *= require bootstrap.min before *= require_self and *= require_tree
  • Peter Berg
    Peter Berg about 11 years
    This didn't work for me, but adding display: table seemed to fix it
  • serv-bot 22
    serv-bot 22 almost 11 years
    Great answer! Working with this from now on.
  • Shawn Taylor
    Shawn Taylor about 10 years
    Instead of adding inline styling (style="text-align:center"), add the class "center-block" and get centred content within your centred div.
  • Gavindra Kalikapersaud
    Gavindra Kalikapersaud almost 10 years
    is that a valid way of centering divs in boostrap?
  • Craig
    Craig almost 10 years
    The content of the div is not centered, but the div itself is.
  • jruzafa
    jruzafa about 8 years
    col-md-4 or col-lg-4 is equal a margin: 0 auto; ?