I'm trying to put a checkbox form in a dropdown like this:
<ul>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
Dropdown Form<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><label class="checkbox"><input type="checkbox">Two</label></li>
<li><label class="checkbox"><input type="checkbox">Two</label></li>
</ul>
</li>
</ul>
Here is a Demo in Bootply
However, as you can see in the demo, for whatever reason the actual checkbox itself appears outside of the dropdown menu. Can anyone tell me what's causing that, and how it should be implemented instead? If I take the label class out it works but it's all bunched up.
Here's what we'll build:
HTML
Essentially, we'll look to combine two different sets of Bootstrap controls & styles: Dropdowns & Checkboxes. Inside of each
li
, we'll use alabel
instead of ana
element, so that we can wrap the checkbox in a label and make the entire row clickable.CSS
We can steal some of the styles normally applied to
.dropdown-menu li a
, input and apply them to our label option instead. We'll make the label occupy the full width of the container and fix some label / checkbox alignment issues. Additionally, we'll add styles for.active
and:hover
.JavaScript
Some other housekeeping, we'll manually keep an
.active
class flag on each list item to correspond to whether or not the item is checked so we can style it appropriately.We'll also want to allow multiple selections by allowing the menu to stay open on internal click events by stopping the event from bubbling up
Demo in Stack Snippets
The way Bootstrap uses checkboxes in their docs is as following:
So yours would look like:
The docs: http://getbootstrap.com/css/#forms
The problem here is that your checkboxes are being styled (by Bootstrap) with:
This is probably done to avoid those "bunching" issues you see when you remove the
.checkbox
class on the<label>
, but that negativemargin-left
is causing problems in this case.One way to address this with minimal adjustment to your markup and CSS would just be to add some padding on the
<label>
to account for it:Here's an updated Bootply to show you the code in action. Hope this helps! Let me know if you have any questions.