Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap 3 button group in panel-header not centering

I have a button group in a panel-header. I want them floated to the right, but when I do this the buttons are now down at the bottom of the header and I need them to be centered. How do I do this?

here's the HTML:

<div class="panel panel-default">
    <div class="panel-heading">
        Member of the following Units
        <div class="btn-group btn-group-sm">
            <button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span></button>
            <button type="button" class="btn btn-danger" ><span class="glyphicon glyphicon-remove-circle"></span></button>
        </div>
    </div>
    <div class="panel-body">
        test
    </div>
    <div class="panel-footer">
        test
    </div>      
</div>

and a fiddle example: http://jsfiddle.net/snowburnt/4ejuK/

Strangely enough, in my linux dev environment on chromium, the buttons themselves are properly centered but the icons within them are lower than they should be, I have a feeling this will answer both these issues.

like image 710
Snowburnt Avatar asked Oct 30 '13 19:10

Snowburnt


People also ask

How do I center a group button 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 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.)

Which class is used to create a button group in Bootstrap?

btn-group class is used to create horizontally arranged button groups.

What is button group?

Button groups allow multiple buttons to be stacked together on a single line. This is useful when you want to place items like alignment buttons together. You can add on optional JavaScript radio and checkbox style behavior with Bootstrap Button Plugin.


2 Answers

  1. You should add the class pull-right to your .btn-group div, instead of specifying float:right.

  2. When you float an element, it loses block layout. It will no longer "push down" the bottom of its container since it doesn't have a height. You can fix this by setting overflow:hidden on your .panel-heading to allow it to resize properly. You will have to add top padding to the .panel-heading and negative top padding to the .btn-group to accomodate the height of the .btn-group.

I forked your fiddle: http://jsfiddle.net/Dq5ge/

like image 125
glomad Avatar answered Oct 20 '22 18:10

glomad


You must clear your floats. There are many methods for this like the clearfix hack or using overflow: hidden. Which is what i did in your fiddle. http://jsfiddle.net/4ejuK/2/

.panel-heading {
    overflow: hidden;
}

parent elements will collapse if their floated children are not cleared causing a lot of unexpected layout issues.

like image 25
Davidicus Avatar answered Oct 20 '22 18:10

Davidicus