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
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.
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.
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.)
Just add the class . text-center to the parent element of the text to center content horizontally.
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="#">«</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="#">»</a></li> </ul> </div>
See working example here: http://jsfiddle.net/EVmwe/409/
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With