Academic Block

CSS Dropdown Get Certified

What is a CSS Dropdown?

A CSS Dropdown is a menu that stays hidden until the user hovers over (or focuses) a trigger element. The extra links then appear below or beside the trigger. Dropdowns are built with plain HTML and CSS using position: relative on the container, position: absolute on the menu, display: none to hide it, and the :hover pseudo-class to reveal it. No JavaScript is required for the basic hover version.

Why Use CSS Dropdown?

Dropdown menus keep navigation bars clean by grouping related pages under one item. They save horizontal space, reduce clutter, and still give users quick access to many links. You will see them on almost every professional website — especially in main navigation bars, account menus, and language selectors.

Basic Dropdown Structure

What this code does: Creates the essential container that every dropdown needs. The container is given position: relative so that a child menu can later be positioned absolutely relative to this box, and display: inline-block so the container only takes as much width as its content.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-dropdown {
    position: relative;
    display: inline-block;
}
</style>
</head>
<body>
<div class="demo-dropdown">
Dropdown Menu
</div>
</body>
</html>

Step 1: Create the Dropdown Button

What this code does: Builds a clean, clickable-looking button that will later serve as the trigger for the dropdown. The button is styled with padding, a solid blue background, white text, and a pointer cursor so students can see the visual foundation before the menu is added.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-btn {
    padding: 12px 20px;
    background: #007BFF;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
}
</style>
</head>
<body>
<button class="demo-btn">Menu</button>
</body>
</html>

Step 2: Create the Hidden Menu

What this code does: Places the dropdown links inside a second container and hides that container with display: none. The menu is positioned absolutely so that when it later becomes visible it will appear directly under the button. At this stage the menu is still invisible — the next step will reveal it on hover.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-dropdown {
    position: relative;
    display: inline-block;
}
.demo-btn {
    padding: 12px 20px;
    background: #007BFF;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
}
.demo-content {
    display: none;
    position: absolute;
    background: #f9f9f9;
    min-width: 180px;
    border: 1px solid #ccc;
    border-radius: 4px;
    top: 100%;
    left: 0;
}
</style>
</head>
<body>
<div class="demo-dropdown">
<button class="demo-btn">Menu</button>
<div class="demo-content">
<a href="#">Home</a>
</div>
</div>
</body>
</html>

Step 3: Show the Menu on Hover

What this code does: Makes the dropdown interactive. When the mouse moves over the entire container (button + area), the menu appears because of the rule .demo-dropdown:hover .demo-content { display: block; }. A soft box-shadow is also added so the menu floats nicely above the page.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-dropdown {
    position: relative;
    display: inline-block;
}
.demo-btn {
    padding: 12px 20px;
    background: #007BFF;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
}
.demo-content {
    display: none;
    position: absolute;
    background: white;
    min-width: 180px;
    border: 1px solid #ccc;
    border-radius: 4px;
    top: 100%;
    left: 0;
    box-shadow: 0 4px 10px rgba(0,0,0,0.15);
}
.demo-content a {
    display: block;
    padding: 10px 14px;
    color: #333;
    text-decoration: none;
}
.demo-dropdown:hover .demo-content {
    display: block;
}
</style>
</head>
<body>
<div class="demo-dropdown">
<button class="demo-btn">Menu</button>
<div class="demo-content">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</div>
</body>
</html>

Step 4: Style the Dropdown Links

What this code does: Polishes the appearance of the menu items. Each link receives generous padding, and a hover state that changes both the background and the text color. The menu also gets a stronger shadow and overflow: hidden so the rounded corners look clean.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-dropdown {
    position: relative;
    display: inline-block;
}
.demo-btn {
    padding: 12px 20px;
    background: #007BFF;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
}
.demo-content {
    display: none;
    position: absolute;
    background: white;
    min-width: 180px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
    border-radius: 4px;
    top: 100%;
    left: 0;
    overflow: hidden;
}
.demo-content a {
    display: block;
    padding: 12px;
    color: black;
    text-decoration: none;
}
.demo-content a:hover {
    background: #007BFF;
    color: white;
}
.demo-dropdown:hover .demo-content {
    display: block;
}
</style>
</head>
<body>
<div class="demo-dropdown">
<button class="demo-btn">Services</button>
<div class="demo-content">
<a href="#">Web Design</a>
<a href="#">SEO</a>
<a href="#">Hosting</a>
</div>
</div>
</body>
</html>

Step 5: Create a Navigation Dropdown

What this code does: Puts a dropdown inside a real navigation bar. The trigger is now a normal navigation link instead of a button, and the whole dropdown sits inside a dark bar. This is the pattern you will use most often on websites.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-nav {
    background: #222;
    padding: 15px;
}
.demo-dropdown {
    display: inline-block;
    position: relative;
}
.demo-dropbtn {
    color: white;
    text-decoration: none;
    padding: 10px 15px;
    display: inline-block;
}
.demo-menu {
    display: none;
    position: absolute;
    background: white;
    min-width: 170px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    border-radius: 4px;
    top: 100%;
    left: 0;
    overflow: hidden;
}
.demo-menu a {
    display: block;
    padding: 12px;
    color: black;
    text-decoration: none;
}
.demo-menu a:hover {
    background: #007BFF;
    color: white;
}
.demo-dropdown:hover .demo-menu {
    display: block;
}
</style>
</head>
<body>
<nav class="demo-nav">
<div class="demo-dropdown">
<a href="#" class="demo-dropbtn">Products</a>
<div class="demo-menu">
<a href="#">Laptop</a>
<a href="#">Mobile</a>
<a href="#">Tablet</a>
</div>
</div>
</nav>
</body>
</html>

Complete CSS Dropdown Example

What this code does: Puts everything together into a polished, reusable dropdown: a styled blue button, a hidden absolute menu with shadow and rounded corners, smooth link hover states, and the pure-CSS hover reveal. This is the complete pattern you can copy into any project.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    margin: 0;
}
.demo-dropdown {
    display: inline-block;
    position: relative;
}
.demo-btn {
    background: #007BFF;
    color: white;
    padding: 12px 20px;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
}
.demo-menu {
    display: none;
    position: absolute;
    min-width: 180px;
    background: white;
    box-shadow: 0 5px 12px rgba(0,0,0,0.2);
    border-radius: 4px;
    top: 100%;
    left: 0;
    overflow: hidden;
}
.demo-menu a {
    display: block;
    padding: 12px;
    color: black;
    text-decoration: none;
}
.demo-menu a:hover {
    background: #007BFF;
    color: white;
}
.demo-dropdown:hover .demo-menu {
    display: block;
}
</style>
</head>
<body>
<div class="demo-dropdown">
<button class="demo-btn">Tutorials</button>
<div class="demo-menu">
<a href="#">HTML</a>
<a href="#">CSS</a>
<a href="#">JavaScript</a>
<a href="#">Python</a>
</div>
</div>
</body>
</html>

Advantages of CSS Dropdown

Advantage
Benefit
Description
Organized Navigation
Easy Browsing
Groups related links under one clean menu item.
Space Saving
Clean Layout
Keeps the main navigation bar short and uncluttered.
Interactive
Better User Experience
Shows extra options only when the user needs them.
No JavaScript Needed
Simple & Fast
Basic hover dropdowns work with pure CSS.

Best Practices

  • Always give the outer container position: relative.
  • Hide>Hide the menu with display: none and show it with :hover.
  • Use position: absolute and top: 100% so the menu appears under the trigger.
  • Make every menu link a block element with good padding for easy clicking.
  • Add a hover style on the links so users know they are interactive.
  • Use a subtle box-shadow to make the menu float above the page.
  • Keep dropdown lists short — long menus become hard to use.
  • Remember that pure hover dropdowns do not work well on touch screens; consider JavaScript for mobile-friendly versions.

Web Resources on CSS Dropdown

1. MDN Web Docs : Customizable select elements
2. CSS Tricks.com : Abusing Customizable Selects
3. CSS Tricks.com : Solved with CSS! Dropdown Menus
4. Dev.to : Dropdown menu with HTML and CSS

🧪 Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

📝 CSS Code
👁️ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.