10

I am using Twitter bootstrap drop down button:

http://getbootstrap.com/components/#btn-dropdowns-single

And I would like to add checkmarks (not necessarily checkboxes) to those list items that the user has selected. Is there style already built in do this or does someone already have a solution built for this?

Thanks!

4
  • 1
    I believe the dropdown auto-closes onclick, so you might want to check that out as well (as in prevent default behavior). Other than that, you can place whatever you want inside the dropdown-menu, so go nuts. Commented Jan 22, 2014 at 15:34
  • Here is a pure css check mark I just made that you can use, jsfiddle.net/d4xNe/1. Otherwise you can just use an image to place inside of it. (I would not suggest a pure css solution since it is impractical) Commented Jan 22, 2014 at 15:37
  • Check out the Bootstrap select plugin -- bootply.com/107784 Commented Jan 22, 2014 at 16:05
  • hey thanks! I will check out that plugin! Commented Jan 27, 2014 at 21:52

4 Answers 4

17

Here's a solution that doesn't require icon fonts or images. This is all the CSS you need:

.dropdown-item-checked::before {
  position: absolute;
  left: .4rem;
  content: '✓';
  font-weight: 600;
}

Toggling the Checked State

To add/remove a checkmark, just toggle the dropdown-item-checked modifier on the appropriate dropdown-item:

<div class="dropdown">
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown">Menu</button>
  <div class="dropdown-menu">
    <a href="#" class="dropdown-item">Item 1</a>
    <a href="#" class="dropdown-item dropdown-item-checked">Item 2</a>
    <a href="#" class="dropdown-item">Item 3</a>
  </div>
</div>

Here's what it looks like:

Bootstra dropdown menu with checked item

Demo & Source

Demo: https://codepen.io/claviska/pen/aLxQgv

Source: https://www.abeautifulsite.net/adding-a-checked-state-to-bootstrap-4-dropdown-menus

Sign up to request clarification or add additional context in comments.

2 Comments

How do I stop this from applying to my other dropdowns or make it apply to one specific one?
Add a class to your dropdown and then prefix the .dropdown-item-checked selector with that class name.
4

Here's a simple solution using a Font Awesome icon: http://www.bootply.com/oJgbiXIzBM

Using the same HTML as http://getbootstrap.com/components/#btn-dropdowns-single

The CSS is:

.active-tick a::after {
    content: '\f00c'; /* http://fontawesome.io/3.2.1/icon/ok/ */
    font-family: 'FontAwesome';
    float: right;
}

And here's an example of mutually exclusive ticks on the options:

$('li').click(function(e) {
    $(this).addClass('active-tick').siblings().removeClass('active-tick');
});

Comments

1

I adapted claviska's great CSS-only answer so that it draws it with a rotated box that has 2 bordered sides, and this provides a more modern, geometric look than an HTML entity would provide.

.dropdown-item-checked::before
{
position: absolute;
left: 0.8rem;
margin-top: 1px;
content: '';
width: 6px;
height: 12px;
border-bottom: 3px solid #333;
border-right: 3px solid #333;
transform: rotate(45deg);
-o-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}

Comments

0

I don't know of anything built into the bootstrap for this.

Firefox and Webkit browser support the background-image property on the option element:

 <option style="background-image:url(check.gif);">Option</option>

Otherwise, you will need to resort to a jQuery solution. This is a nice one:

http://www.marghoobsuleman.com/jquery-image-dropdown

1 Comment

Cant access the above link...looks like it is hacked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.