YogeshChauhan . com

How to create a simple drop down menu using JavaScript and CSS?

in CSS & JavaScript on March 17, 2021

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:

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to insert Bootstrap 4 in Angular 9 app?AngularThe difference between 400(4xx) and 500(5xx) errorsMiscSocial Media Colors: Sass VariablesMiscFETCH clause in PostgreSQLPostgresHow to create a flip pricing table using CSS and JavaScript?CSSHow to get the full URL of current page in PHP?PHPHow to replace HTML lists using CSS Counters?CSSHow to render Lists in React?ReactHow to avoid element shift on border change while hovering in CSS?CSS2 Ways We Can Write Multiple Line Commands in PHPPHPHow to get ACF values from custom post type?WordPressIN and BETWEEN Operators in SQLSQL/MySQL