In this tutorial we will see how to create a Simple Dropdown Menu with Pure CSS. Simple HTML and CSS is used for this purpose and complete code is given.
Table of Contents
HTML Code
Take a look at the HTML code given below.
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title>Dropdown Menu using HTML and CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<details class="dropdown">
<summary role="button">
<a class="button">Menu</a>
</summary>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Dashboard</a></li>
<li><a href="#">Settings</a></li>
<li><a href="#">Logout</a></li>
</ul>
</details>
</body>
</html>
CSS Code
CSS code is given below.
<style>
body {
background-color: #444;
text-align: center;
font-family: monospace;
}
a.button {
display: inline-block;
padding: 10px 30px;
border-radius: 50px;
box-sizing: border-box;
border: none;
background: #22438C;
color: #ddd;
font-size: 20px;
cursor: pointer;
position: relative;
}
.dropdown {
position: absolute;
padding: 0;
margin-right: 1em;
border: none;
}
.dropdown summary {
list-style: none;
list-style-type: none;
}
.dropdown summary:focus a.button {
border: 2px solid #ddd;
}
.dropdown summary:focus {
outline: none;
}
.dropdown ul {
position: absolute;
margin: 20px 0 0 0;
padding: 0;
width: 120px;
left: 50%;
margin-left: calc(120px / 2 * -1);
box-sizing: border-box;
z-index: 2;
background: #ddd;
border-radius: 6px;
list-style: none;
}
.dropdown ul li {
padding: 0;
margin: 0;
}
.dropdown ul li a:link, .dropdown ul li a:visited {
display: inline-block;
padding: 10px 0.8rem;
width: 100%;
box-sizing: border-box;
color: #000;
text-decoration: none;
}
.dropdown ul li a:hover {
background-color: #22438C;
color: #ddd;
}
.dropdown ul::before {
content: ' ';
position: absolute;
width: 0;
height: 0;
top: -10px;
left: 50%;
margin-left: -10px;
border-style: solid;
border-width: 0 10px 10px 10px;
border-color: transparent transparent #ddd transparent;
}
.dropdown > summary::before {
display: none;
}
.dropdown[open] > summary::before {
content: ' ';
display: block;
position: fixed;
top: 0;
right: 0;
left: 0;
bottom: 0;
z-index: 1;
}
</style>
Video Tutorial
Watch our video tutorial on Simple Dropdown Menu with Pure CSS.