Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center .btn-group from twitter-bootstrap?

I'm using twitter-bootstrap, and i found it fairly hard to center div which has class .btn-group (link). Usually, I use

margin: 0 auto;  

or

text-align: center; 

to center stuff within div, but it doesn't work when inner element has property float: left which in this case uses for visual effects.

http://jsfiddle.net/EVmwe/1

like image 293
evfwcqcg Avatar asked Mar 24 '12 17:03

evfwcqcg


People also ask

How do I center my BTN Group in bootstrap?

Answer: Use the text-center Class You can simply use the built-in class . text-center on the wrapper element to center align buttons or other inline elements in Bootstrap. It will work in both Bootstrap 3 and 4 versions.

How do I align my BTN Center?

We can align the buttons horizontally as well as vertically. We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.

How do you center a group button in HTML?

You can center a block level element by setting margin-left and margin-right to auto . We can use the shorthand margin: 0 auto to do this. (This also sets margin-top and margin-bottom to zero.)

How do I center a horizontal in bootstrap?

Just add the class . text-center to the parent element of the text to center content horizontally.


2 Answers

Edited: This has changed as of Bootstrap 3. See solution for Bootstrap 3 below.

Is adding a wrapping div out of the question?

Look at this example: http://jsfiddle.net/EVmwe/4/

Important part of the code:

/* a wrapper for the paginatior */ .btn-group-wrap {     text-align: center; }  div.btn-group {     margin: 0 auto;      text-align: center;     width: inherit;     display: inline-block; }  a {     float: left;    } 

Wrapping the button group within a division, and setting the div to text-align center. The button group must also be overwritten to have display inline-block and inherited width.

Overwriting the anchors display to inline-block, and float: none, would also probably work, as @Andres Ilich said.


Update for Bootstrap 3

As of Bootstrap 3, you have alignment classes (as seen in the documentation). You now simply have to add the text-center class to a parent. Like so:

<div class="text-center">     <ul class="pagination">       <li class="disabled"><a href="#">&laquo;</a></li>       <li class="active"><a href="#">1</a></li>       <li><a href="#">2</a></li>       <li><a href="#">3</a></li>       <li><a href="#">4</a></li>       <li><a href="#">5</a></li>       <li><a href="#">&raquo;</a></li>     </ul> </div> 

See working example here: http://jsfiddle.net/EVmwe/409/

like image 100
mikaelb Avatar answered Sep 27 '22 19:09

mikaelb


Adding text-center to a parent works for me (Bootstrap 3):

<div class="text-center">    <div class="btn-group">       <a href=1 class="btn btn-small">1</a>       <a href=1 class="btn btn-small">2</a>       <a href=1 class="btn btn-small">3</a>    </div> </div> 
like image 20
Alberto Gaona Avatar answered Sep 27 '22 19:09

Alberto Gaona