How TO - Clickable DropdownLearn how to create a clickable dropdown menu with CSS and JavaScript. Show A dropdown menu is a toggleable menu that allows the user to choose one value from a predefined list: Try it Yourself » Create a Clickable DropdownCreate a dropdown menu that appears when the user clicks on a button. Step 1) Add HTML:Example<div class="dropdown"> Example ExplainedUse any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS. Step 2) Add CSS:Example /* Dropdown Button */ /* Dropdown button on hover & focus */ /* The container <div> - needed to position the dropdown content */ /* Dropdown Content (Hidden by Default) */ /* Links inside the dropdown */ /* Change color of dropdown links on hover */ /* Show the dropdown menu (use JS to add this class to the
.dropdown-content container when the user clicks on the dropdown button) */ Example ExplainedWe have styled the dropdown button with a background-color, padding, hover effect, etc. The The Instead of using a border, we have used the Step 3) Add JavaScript:Example /* When the user clicks on the button, // Close the dropdown menu if the user clicks outside of it Try it Yourself » Right-aligned dropdownDropdown Menu in NavbarSearch (Filter) DropdownTip: Go to our CSS Dropdowns Tutorial to learn more about dropdowns. Tip: Go to our Hoverable Dropdowns to learn more about hoverable dropdowns Learn how to create a hoverable dropdown menu with CSS. DropdownA dropdown menu is a toggleable menu that allows the user to choose one value from a predefined list: Try it Yourself » Create A Hoverable DropdownCreate a dropdown menu that appears when the user moves the mouse over an element. Step 1) Add HTML:Example<div class="dropdown"> Example ExplainedUse any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS. Step 2) Add CSS:Example /* Dropdown Button */ /* The container <div> - needed to position the dropdown content */ /* Dropdown Content (Hidden by Default) */ /* Links inside the dropdown */ /* Change color of dropdown links on hover */ /* Show the dropdown menu on hover */ /* Change the background color of the dropdown button when the dropdown content is shown */ Try it Yourself » Example ExplainedWe have styled the dropdown button with a background-color, padding, etc. The The Instead of using a border, we have used the The
Right-aligned dropdownDropdown Menu in NavbarTip: Go to our CSS Dropdowns Tutorial to learn more about dropdowns. Tip: Go to our Clickable Dropdowns to learn more about clickable dropdowns |