In this tutorial we will see how to create a Dropdown Menu with Search bar using HTML and CSS. Simple HTML and CSS is used for this purpose, 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 with Search using HTML and CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav>
<ul class="primary">
<li><a href="#">Menu 1</a></li>
<li><a href="#">Menu 2</a>
<ul class="sub">
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
</ul>
</li>
<li><a href="#">Menu 3</a></li>
<li><a href="#">Menu 4</a></li>
</ul>
<input type="search" class="search" placeholder="Search">
</nav>
</body>
</html>
CSS Code
CSS code is given below.
<style>
body{
background: #ccc;
font-family: monospace;
margin: 0;
}
ul{
list-style: none;
padding: 0;
margin: 0;
}
nav{
background: #4000ad;
position: relative;
padding: 0px 20px;
}
.search
{
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 0%;
margin-right: 20px;
padding: 10px;
border-radius: 5px;
}
a{
text-decoration: none;
color: #fff;
display: block;
padding: 20px;
}
li{
float: left;
font-size: 15px;
}
ul:after{
clear: both;
}
ul:before,
ul:after {
content: " ";
display: table;
}
ul.primary li a:hover {
color: #000;
}
ul.sub {
position: absolute;
z-index: 200;
box-shadow: 2px 2px 0 #BEBEBE;
width: 35%;
display:none;
}
ul.sub li {
float: none;
margin: 0;
}
ul.sub li a {
border-top: 1px solid #fff;
border-right: none;
padding: 15px 30px;
}
ul.sub li:last-child a {
border-bottom: none;
}
ul.primary li:hover ul {
display: block;
}
ul.primary li:hover a {
background: #fff;
color: #666;
text-shadow: none;
}
ul.primary li:hover > a{
color: #000;
}
ul.sub li a:hover {
color: #fff;
background: #4000ad;
}
</style>
Video Tutorial
Watch our video tutorial on Dropdown Menu with Search bar using HTML and CSS.