Dropdown (Requires JavaScript)
Dropdowns can be used for toggleable, contextual overlays (menus) that display links, forms, and other content.
Commenting
|
New message!
Jane Doe just sent you a new message.
|
Dropdowns can be used for toggleable, contextual overlays (menus) that display links, forms, and other content.
Click on the following button to open a dropdown menu.
<div class="dropdown">
<button class="btn" data-toggle="dropdown" type="button" id="dropdown-toggle-btn-1" aria-haspopup="true" aria-expanded="false">
Click me <i class="fa fa-angle-down ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu" aria-labelledby="dropdown-toggle-btn-1">
<h6 class="dropdown-header">Header</h6>
<a href="#" class="dropdown-item">Link 1</a>
<a href="#" class="dropdown-item">Link 2</a>
<div class="dropdown-divider"></div>
<div class="dropdown-content">
<button class="btn btn-block" type="button">Button</button>
</div>
</div>
</div>
<!-- Requires halfmoon.js -->
<script src="path/to/halfmoon.js"></script>
When an element with the attribute data-toggle="dropdown" and an element with the class .dropdown-menu are placed inside a parent container element with the class .dropdown, the system will toggle the dropdown menu whenever the toggle is clicked. This works by adding/removing the class .active to/from the toggle, and by adding/removing the class .show to/from the parent container element. The dropdown toggle does not have to be a button, it can be any other appropriate element, for example, links, links styled as buttons, navbar items, etc.
The following can be used inside the dropdown menu:
On a side note, the icon being used is part of the Font Awesome 4.7.0 iconset.
Dropdowns with the class .with-arrow will have a tiny arrow indicating where the dropdown menu pops in from.
<!-- Dropdown with arrow -->
<div class="dropdown with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Click me <i class="fa fa-angle-down ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu" aria-labelledby="...">
...
</div>
</div>
<!-- Requires halfmoon.js -->
<script src="path/to/halfmoon.js"></script>
By default, dropdown menus are aligned to the left. However, they can can be aligned alternatively in the following ways:
<!-- Default dropdown menu -->
<div class="dropdown with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Default <i class="fa fa-angle-down ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu" aria-labelledby="...">
...
</div>
</div>
<!-- Dropdown menu aligned to the center -->
<div class="dropdown with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Center <i class="fa fa-angle-down ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-center" aria-labelledby="...">
...
</div>
</div>
<!-- Dropdown menu aligned to the right -->
<div class="dropdown with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Right <i class="fa fa-angle-down ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="...">
...
</div>
</div>
<!-- Requires halfmoon.js -->
<script src="path/to/halfmoon.js"></script>
Dropdown menus can be made to open up (instead of down) by adding the class .dropup to the dropdown container (.dropdown). Once again, these menus can be aligned alternatively in the following ways:
<!-- Default dropup menu -->
<div class="dropdown dropup with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Default <i class="fa fa-angle-up ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu" aria-labelledby="...">
...
</div>
</div>
<!-- Dropup menu aligned to the center -->
<div class="dropdown dropup with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Center <i class="fa fa-angle-up ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-center" aria-labelledby="...">
...
</div>
</div>
<!-- Dropup menu aligned to the right -->
<div class="dropdown dropup with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Right <i class="fa fa-angle-up ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="...">
...
</div>
</div>
<!-- Requires halfmoon.js -->
<script src="path/to/halfmoon.js"></script>
Dropdown menus can be made to open to the right by adding the class .dropright to the dropdown container (.dropdown). By default, these menus will be aligned vertically towards the downward direction. However, they can be aligned alternatively in the following ways:
<!-- Default dropright menu -->
<div class="dropdown dropright with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Default <i class="fa fa-angle-right ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu" aria-labelledby="...">
...
</div>
</div>
<!-- Dropright menu aligned to the center (vertically) -->
<div class="dropdown dropright with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Center <i class="fa fa-angle-right ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-center" aria-labelledby="...">
...
</div>
</div>
<!-- Dropright menu aligned towards the upward direction -->
<div class="dropdown dropright with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
Upward <i class="fa fa-angle-right ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-up" aria-labelledby="...">
...
</div>
</div>
<!-- Requires halfmoon.js -->
<script src="path/to/halfmoon.js"></script>
Dropdown menus can be made to open to the left by adding the class .dropleft to the dropdown container (.dropdown). By default, these menus will be aligned vertically towards the downward direction. However, they can be aligned alternatively in the following ways:
<!-- Default dropleft menu -->
<div class="dropdown dropleft with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
<i class="fa fa-angle-left mr-5" aria-hidden="true"></i> Default <!-- mr-5 = margin-right: 0.5rem (5px) -->
</button>
<div class="dropdown-menu" aria-labelledby="...">
...
</div>
</div>
<!-- Dropleft menu aligned to the center (vertically) -->
<div class="dropdown dropleft with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
<i class="fa fa-angle-left mr-5" aria-hidden="true"></i> Center <!-- mr-5 = margin-right: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-center" aria-labelledby="...">
...
</div>
</div>
<!-- Dropleft menu aligned towards the upward direction -->
<div class="dropdown dropleft with-arrow">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
<i class="fa fa-angle-left mr-5" aria-hidden="true"></i> Upward <!-- mr-5 = margin-right: 0.5rem (5px) -->
</button>
<div class="dropdown-menu dropdown-menu-up" aria-labelledby="...">
...
</div>
</div>
<!-- Requires halfmoon.js -->
<script src="path/to/halfmoon.js"></script>
As mentioned above, other content (such as inputs, buttons, and forms) can be placed in dropdown menus by putting them inside an element with the class .dropdown-content. This will give them the appropriate padding to match with the headers, links and dividers. This along with simple utility classes can be used to create some very useful dropdown menus.
<!-- Displaying other content -->
<div class="dropdown">
<button class="btn btn-primary" data-toggle="dropdown" type="button" id="sign-in-dropdown-toggle-btn" aria-haspopup="true" aria-expanded="false">
Sign in <i class="fa fa-angle-down ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu w-250 w-sm-350" aria-labelledby="sign-in-dropdown-toggle-btn"> <!-- w-250 = width: 25rem (250px), w-sm-350 = width: 35rem (350px) only on devices where width > 576px -->
<div class="dropdown-content p-20"> <!-- p-20 = padding: 2rem (20px) -->
<form action="..." method="...">
<div class="form-group">
<label for="username" class="required">Username</label>
<input type="text" name="username" class="form-control" placeholder="Username" required="required">
</div>
<div class="form-group">
<label for="password" class="required">Password</label>
<input type="password" name="password" class="form-control" placeholder="Password" required="required">
</div>
<input class="btn btn-primary btn-block" type="submit" value="Sign in">
<div class="text-right mt-10"> <!-- text-right = text-align: right, margin-top: 1rem (10px) -->
<a href="#" class="hyperlink">Forgot password?</a> <!-- hyperlink = used on regular links to remove anti-aliasing in dark mode -->
</div>
</form>
</div>
<div class="dropdown-divider"></div>
<a href="#" class="dropdown-item text-center">Don't have an account? Sign up</a> <!-- text-center = text-align: center -->
</div>
</div>
<!-- Requires halfmoon.js -->
<script src="path/to/halfmoon.js"></script>
Since the width of the dropdown menu depends on the width of the widest element within it, it is often a good idea to set the width using sizing utility classes (opens in new tab), as can be seen in the above example.
Dropdown menus can also be made to toggle on hover by adding the class .toggle-on-hover to the dropdown container (.dropdown). These dropdown menus will stay open as long as the toggle or the menu is hovered over.
While they require no JavaScript, it is still recommended that the data-toggle="dropdown" attribute is used, so that the menu can be toggled by both hover and clicks on the toggle.
<!-- Dropdown that can be toggled by both hover and clicks on the toggle -->
<div class="dropdown toggle-on-hover">
<button class="btn" data-toggle="dropdown" type="button" id="..." aria-haspopup="true" aria-expanded="false">
On hover <i class="fa fa-angle-down ml-5" aria-hidden="true"></i> <!-- ml-5 = margin-left: 0.5rem (5px) -->
</button>
<div class="dropdown-menu" aria-labelledby="...">
...
</div>
</div>
<!-- Required only if the data-toggle="dropdown" attribute is used on the toggles -->
<script src="path/to/halfmoon.js"></script>
It should be pretty obvious from the various examples found on this page that dropdowns can be used as popovers. For instance, the next example shows a popover (dropright) that opens when the user clicks on the avatar.
<!-- Popover (dropright) that opens when the user clicks on the avatar -->
<div class="dropdown dropright with-arrow">
<a href="#" data-toggle="dropdown" id="avatar-popover-toggle" aria-haspopup="true" aria-expanded="false">
<img src="..." class="rounded-circle w-50 h-50" alt="..."> <!-- rounded-circle = border-radius: 50%, w-50 = width: 5rem (50px), h-50 = height: 5rem (50px) -->
</a>
<div class="dropdown-menu dropdown-menu-center" aria-labelledby="avatar-popover-toggle">
<h6 class="dropdown-header">Jane Doe</h6>
<div class="dropdown-divider"></div>
<a href="#" class="dropdown-item">See activity</a>
<a href="#" class="dropdown-item">Add as friend</a>
</div>
</div>
Dropdowns are very useful elements which can be used in many different places. For example, inside input groups (opens in new tab), and in navbars (opens in new tab). The important thing to remember is that the wrapping container with the .dropdown class is always required.
Our goal is to make Halfmoon the go-to framework for building dashboards and tools on the web. We are already working on things like a form validator, number input, datepicker, multi-select (think Select2 without jQuery), tabs component, JSX docs, and so much more. You can learn more in the project roadmap.
However, we need your help to grow. So if you truly believe in our goal, please consider supporting us through Patreon.
The following is the highest tier, with a maximum of 1 sponsor.
(Also seen at the top of the page)
The following is the normal tier, with a maximum of 10 sponsors.
AYCD
Automation Done Right
Sponsor (9 available)
Halfmoon needs your support! Become a sponsor to place an image and description of your product here. Click here
The following people have generously decided to support Halfmoon.
Become a backer on Patreon .