Creating a drop down menu has one simple logic. Show the drop down box when clicked(hovered) on parent element. There are multiple ways to do that, even using just CSS. But in this example I am gonna use few lines of JavaScript to capture the onclick event.
Let’s start with simple HTML and create the container with a button to click on.
Now, let’s add the style for that button and the container. We’ll keep the position of the container relative.
Let’s add the menu items container inside the main container.
Now, let’s add style for that menu container. We need to keep the position absolute. I have kept the menu items style same as the button but feel free to modify it.
Now, the only thing left to do is adding a class and toggle it using JavaScript onclick event.
Let’s add just few lines of JavaScript code and the CSS class as well.
Here’s the full code and demo:
display drop down menu