Open In App
Related Articles

How to show/hide dropdown menu on mouse hover using CSS ?

Improve Article
Improve
Save Article
Save
Like Article
Like

The approach of this article is to show and hide the dropdown menu on mouse hover using CSS. The task can be done by using the display property and :hover selector.

Example:  

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        How to Show Hide Dropdown Using CSS
    </title>
 
    <style>
        ul {
            padding: 0;
            list-style: none;
            background: #f2f2f2;
            width: 500px;
        }
 
        ul li {
            display: block;
            position: relative;
            line-height: 21px;
            text-align: left;
        }
        ul li a {
            display: block;
            padding: 8px 25px;
            color: #333;
            text-decoration: none;
        }
        ul li a:hover {
            color: #fff;
            background: #939393;
        }
        ul li ul.dropdown {
            min-width: 100%;
            /* Set width of the dropdown */
            background: #f2f2f2;
            display: none;
            position: absolute;
            z-index: 999;
            left: 0;
        }
        ul li:hover ul.dropdown {
            display: block;
            /* Display the dropdown */
        }
    </style>
</head>
 
<body>
    <h1>
        GeeksForGeeks
    </h1>
 
    <h2>
        How to show and hide dropdown
        menu on mouse hover using CSS?
    </h2>
 
    <ul>
        <li>
            <a href="#">Programming languages</a>
            <ul class="dropdown">
                <li><a href="#">C++</a></li>
                <li><a href="#">JAVA</a></li>
                <li><a href="#">PHP</a></li>
            </ul>
        </li>
    </ul>
 
</body>
 
</html>


Output:

 

Supported Browsers are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

CSS is the foundation of web pages and is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 Jun, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials