Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a div with Bootstrap2?

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...

like image 387
Alex Avatar asked Feb 21 '12 18:02

Alex


People also ask

How do I center a specific div?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I center a div with margin auto?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


1 Answers

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.

like image 124
Zuhaib Ali Avatar answered Oct 05 '22 04:10

Zuhaib Ali