Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a buttons group with one button having dropdown menu?

How to create a buttons group with one button having dropdown menu? I ask because in this situation the menu appears not under/below the button but at the left page border. If I remove the group-button class(class="btn-group") (I get the standalone button) the dropdown appears correctly? Is there a way how to manage this?

Here is the link to buttons group manual page just to reference to what I mean. http://twitter.github.com/bootstrap/components.html#buttonGroups

like image 981
David Marko Avatar asked Mar 08 '12 13:03

David Marko


People also ask

How do I add a button to a dropdown list?

To add a dropdown to a button, simply wrap the button and dropdown menu in a . btn-group. You can also use <span class = "caret"></span> to act as an indicator that the button is a dropdown.

Does button group allows multiple buttons to be stacked together on a single line?

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. This class is used for a basic button group.

What is a button group?

Button groups ( javax. swing. ButtonGroup ) are used in combination with radio buttons to ensure that only one radio button in a group of radio buttons is selected. To visualize the grouping, JFormDesigner displays lines connecting the grouped buttons.

Which class is used with drop down menu to open left of the button?

Example Explained dropdown class indicates a dropdown menu. To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. The .


2 Answers

BOOTSTRAP 4:

Nested Button Groups

<div class="btn-group" role="group" aria-label="Button group with nested dropdown">
  <button type="button" class="btn btn-secondary">1</button>
  <button type="button" class="btn btn-secondary">2</button>

  <div class="btn-group" role="group">
    <button id="btnGroupDrop1" type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown
    </button>
    <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
      <a class="dropdown-item" href="#">Dropdown link</a>
      <a class="dropdown-item" href="#">Dropdown link</a>
    </div>
  </div>
</div>

BOOTSTRAP 3:

The right answer for bootstrap 3.0 is here:

Nested Button Groups

No need to change CSS:

<div class="btn-group">
  <button type="button" class="btn btn-default">1</button>
  <button type="button" class="btn btn-default">2</button>

  <div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
      Dropdown
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      <li><a href="#">Dropdown link</a></li>
      <li><a href="#">Dropdown link</a></li>
    </ul>
  </div>
</div>
like image 142
tommasop Avatar answered Oct 11 '22 15:10

tommasop


I add a custom "group-dropdown" class to the "btn-group" div wrapping my dropdown button. In this way, the dropdown's "dropdown-toggle" can be overriden by the custom CSS.

Position in the group is handled by :first-child and :last-child

Here's the hack : http://jsfiddle.net/Dk7sw/6/ (LESS CODE available too at CSS bottom)

CSS :

/*

Twitter Bootstrap CSS modifications for having dropdown menus in
buttons groups.
Dropdowns menus are well positionned, under their buttons.

LESS code at bottom

Kure2012.

*/
.btn-group.group-dropdown {
  margin: 0px;
  padding: 0px;
  margin-left: -1px;
}
.btn-group.group-dropdown .dropdown-toggle {
  -webkit-border-radius: 0;
  -moz-border-radius: 0;
  border-radius: 0;
}
.btn-group.group-dropdown:first-child {
  margin: 0px;
  padding: 0px;
  margin-right: -1px;
}
.btn-group.group-dropdown:first-child .dropdown-toggle {
  -webkit-border-top-left-radius: 4px;
  -moz-border-radius-topleft: 4px;
  border-top-left-radius: 4px;
  -webkit-border-bottom-left-radius: 4px;
  -moz-border-radius-bottomleft: 4px;
  border-bottom-left-radius: 4px;
}
.btn-group.group-dropdown:first-child .dropdown-toggle.btn-large {
  -webkit-border-top-left-radius: 6px;
  -moz-border-radius-topleft: 6px;
  border-top-left-radius: 6px;
  -webkit-border-bottom-left-radius: 6px;
  -moz-border-radius-bottomleft: 6px;
  border-bottom-left-radius: 6px;
}
.btn-group.group-dropdown:last-child {
  margin: 0px;
  padding: 0px;
  margin-left: -1px;
}
.btn-group.group-dropdown:last-child .dropdown-toggle {
  -webkit-border-top-right-radius: 4px;
  -moz-border-radius-topright: 4px;
  border-top-right-radius: 4px;
  -webkit-border-bottom-right-radius: 4px;
  -moz-border-radius-bottomright: 4px;
  border-bottom-right-radius: 4px;
}
.btn-group.group-dropdown:last-child .dropdown-toggle.btn-large {
  -webkit-border-top-right-radius: 6px;
  -moz-border-radius-topright: 6px;
  border-top-right-radius: 6px;
  -webkit-border-bottom-right-radius: 6px;
  -moz-border-radius-bottomright: 6px;
  border-bottom-right-radius: 6px;
}

/* And the LESS Code :
.btn-group{
    &.group-dropdown{
        margin:0px;
        padding:0px;
        margin-left:-1px;
        .dropdown-toggle{
            .border-radius(0);
        }
        &:first-child{
            margin:0px;
            padding:0px;
            margin-right:-1px;
            .dropdown-toggle{
                .border-top-left-radius(4px);
                .border-bottom-left-radius(4px);
                &.btn-large{
                    .border-top-left-radius(6px);
                    .border-bottom-left-radius(6px);
                }
            }                
        }
        &:last-child{
            margin:0px;
            padding:0px;
            margin-left:-1px;
            .dropdown-toggle{
                .border-top-right-radius(4px);
                .border-bottom-right-radius(4px);
                &.btn-large{
                    .border-top-right-radius(6px);
                    .border-bottom-right-radius(6px);
                }
            }
        }
    }    
}

And HTML :

<!--

Custom HTML : add a "group-dropdown" class to the dropdown
"btn-group" div.
The "btn-group" div must wrap the whole button (button+submenu items), as specified in the bootstrap doc. This allows you to put several dropdowns in the same buttons group.

Works whatever their position in the button group is.

-->

<!---
Here starts the button group
-->
<div class="btn-group">
    <!--First dropdown : note the "group-dropdown" property -->
    <div class="btn-group group-dropdown">
        <a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#" title="Filter by">
            <i class="icon icon-filter"></i>
            <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
            <li><a href="#">All</a></li>
            <li><a href="#">...</a></li>
        </ul>
    </div>

    <!-- Classic button -->
    <a href="#" class="btn btn-mini" title="New">
        <i class="icon-plus-sign"></i>
    </a>

    <!-- Classic button -->
    <a href="#" class="btn btn-mini" title="Import">
        <i class="icon-upload"></i>
    </a>

    <!-- Dropdown in the middle of the button group -->
    <div class="btn-group group-dropdown">
        <a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#" title="Export">
            <i class="icon icon-download"></i>
            <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
            <li><a href="#">.csv file</a></li>
            <li><a href="#">.json file</a></li>
            <li><a href="#">.ods file</a></li>
            <li><a href="#">.xml file</a></li>
            <li><a href="#">.sql file</a></li>
        </ul>
    </div>

    <!-- Dropdown at the end of the button group -->
    <div class="btn-group group-dropdown">
        <a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#" title="Another action">
            <i class="icon icon-home"></i>
            <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
            <li><a href="#">Custom menu</a></li>
            <li><a href="#">Custom menu</a></li>
            <li><a href="#">Custom menu</a></li>
        </ul>
    </div>

</div>
like image 30
Kure Avatar answered Oct 11 '22 15:10

Kure