Dropdown
A simple and interactive dropdown menu
The dropdown element consists of 3 core components; a wrapper element, a visible trigger and the actual dropdown block.
- Link item 1
- Link item 2
- Link item 3
- A block of text or custom content
You can create the wrapper element with the has-dropdown
modifier. This can be applied to the div and li tags, or most other HTML elements.
<div class="has-dropdown"> </div>
The visible trigger can be any HTML element, though common use cases are either buttons or link tags.
<div class="has-dropdown"> <button class="button is-primary has-arrow">Dropdown button</button> </div>
Use the has-arrow
modifier to display a dropdown arrow. This can be applied to the trigger element or the wrapper element.
The dropdown block is created with the dropdown
class and the following elements:
dropdown__link
will give the list item clickable traits.dropdown__item
is used for displaying non-clickable content or blocks of text.dropdown__divider
a dividing line.
<div class="has-dropdown"> <button class="button is-primary has-arrow">Dropdown button</button> <ul class="dropdown"> <li class="dropdown__link"><a href="#">Link item 1</a></li> <li class="dropdown__link"><a href="#">Link item 2</a></li> <li class="dropdown__link"><a href="#">Link item 3</a></li> <li class="dropdown__divider"></li> <li class="dropdown__link">A block of text or custom content</li> </ul> </div>
Triggers
Hover
The is-hoverable
modifier provides a pure CSS solution for toggling your dropdown menus.
<div class="has-dropdown is-hoverable"> <button class="button is-primary has-arrow">Hover me</button> <ul class="dropdown"> <li class="dropdown__link"><a href="#">Link item 1</a></li> <li class="dropdown__link"><a href="#">Link item 2</a></li> <li class="dropdown__link"><a href="#">Link item 3</a></li> </ul> </div>
</> Show code
Active
The is-active
modifier will also toggle the dropdown menu. To make use of this you will have to write your own JavaScript. You'll find a basic-level example below.
<div class="has-dropdown"> <button class="button is-primary has-arrow" onclick="this.closest('.has-dropdown').classList.toggle('is-active');">Click me</button> <ul class="dropdown"> <li class="dropdown__link"><a href="#">Link item 1</a></li> <li class="dropdown__link"><a href="#">Link item 2</a></li> <li class="dropdown__link"><a href="#">Link item 3</a></li> </ul> </div>
</> Show code
Right alignment
You can use the is-right
modifier for a right-aligned dropdown menu.
- This is a right aligned dropdown.
<div class="has-dropdown is-active is-right"> <button class="button is-primary has-arrow">Right aligned</button> <ul class="dropdown"> <li class="dropdown__item">This is a right aligned dropdown.</li> </ul> </div>
CSS variables
You can customize the dropdown elements with the following CSS variables.
.dropdown { --background-color: var(--color-white); --font-color: var(--color-black); --border-color: hsl(0deg, 0%, 80%, 0.34); --border-width: 1px; --border-radius: 0; --box-shadow: 0 6px 6px hsl(0deg, 0%, 4%, 0.16); --min-width: 12rem; --padding: 10px 0; } /* Dropdown link */ .dropdown__link { --font-color: inherit; --padding: 5px 15px; --hover-font-color: inherit; --hover-background-color: hsl(0deg, 0%, 75%, 0.09); } /* Dropdown divider */ .dropdown__divider { --color: hsl(210deg, 16%, 92%, 1); --margin: 5px 0; } /* Dropdown item */ .dropdown__item { --font-color: inherit; --padding: 5px 15px; }
Sass variables
The default styles can be overridden with the following variables. Set one or more of these variables before you import the framework.
Variable | Type | Default value |
---|---|---|
$dropdown-background | color | hsl(0deg, 0%, 100%, 1) |
$dropdown-color | color | hsl(0deg, 0%, 22%, 1) |
$dropdown-border-width | units | 1px |
$dropdown-border-color | color | hsl(0deg, 0%, 80%, 0.34) |
$dropdown-border-radius | units | 0 |
$dropdown-shadow | box-shadow | 0 6px 6px hsl(0deg, 0%, 4%, 0.16) |
$dropdown-min-width | units | 12rem |
$dropdown-padding | padding | 10px 0 |
$dropdown-link-color | color | inherit |
$dropdown-link-padding | padding | 5px 15px |
$dropdown-link-hover-color | color | inherit |
$dropdown-link-hover-background | color | hsl(0deg, 0%, 75%, 0.09) |
$dropdown-divider-color | color | hsl(210deg, 16%, 92%, 1) |
$dropdown-divider-margin | margin | 5px 0 |
$dropdown-item-color | color | inherit |
$dropdown-item-padding | padding | 5px 15px |
$dropdown-arrow-color | color | hsl(232, 100%, 72%, 1) |