How to center a div with Bootstrap2?

前端 未结 9 1106
星月不相逢
星月不相逢 2020-12-04 09:16

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

I tried like all combinations:

b
相关标签:
9条回答
  • 2020-12-04 10:03

    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>
    
    0 讨论(0)
  • 2020-12-04 10:07

    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.

    0 讨论(0)
  • 2020-12-04 10:07

    add the class centercontents

    /** Center the contents of the element **/
    .centercontents {
        text-align: center !important;
    }
    
    0 讨论(0)
提交回复
热议问题