Skip to content

Responsive Dropdown Navbar using HTML and CSS

Easy method to make a responsive dropdown navbar using HTML and CSS. anyone can make this.

Index.html

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="css/style.css">
</head>

<body>

  <div class="topnav" id="myTopnav">
    <a href="#home" class="active"><span class="fa fa-home"></span>Home</a>
    <a href="#news"><span class="fa fa-user"></span>Profile</a>

    <div class="dropdown">
      <button class="dropbtn"><span class="fa fa-envelope"></span>Massage
        <i class="fa fa-caret-down"></i>
      </button>
      <div class="dropdown-content">
        <a href="#"><span class="fa fa-caret-right"></span>Inbox</a>
        <a href="#"><span class="fa fa-caret-right"></span>Sent</a>
        <a href="#"><span class="fa fa-caret-right"></span>Draft</a>
      </div>
    </div>
    <a href="#about"><span class="fa fa-cog"></span>Settings</a>
    <a href="#contact"><span class="fa fa-power-off"></span>Logout</a>
    <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
  </div>

  <div class="body">
      <h1>Reponsive Dropdown Navbar using HTML and CSS</h1>
  </div>

  <script>
    function myFunction() {
      var x = document.getElementById("myTopnav");
      if (x.className === "topnav") {
        x.className += " responsive";
      } else {
        x.className = "topnav";
      }
    }
  </script>

</body>

</html>

style.css

@import url(https:fonts.googleapis.com/css/css?family=Open+Sans:400,300,300italic,400italic,600italic,600,700,700italic,800,800italic);

*{
  margin: 0;
  padding: 0;
}

body{
  font-family: 'Open Sans',sans-serif;
  background: #f1c40f;
}


.topnav, .body{
  width: 90%;
  margin: 0 auto;
  margin-top: 5%;
  border: 3px solid;

  border-color: rgba(124,124,124,0.5);
  border-radius: 7px;
  -moz-border-radius:7px;
  -webkit-border-radius:7px;
  -webkit-box-shadow:0px 0px 18px 4px rgba(44,48,57,0.2);
  -moz-box-shadow:0px 0px 18px 4px rgba(44,48,57,0.2);
  box-shadow:0px 0px 18px 4px rgba(44,48,57,0.2);
  overflow: hidden;
  background: #383d49;
  display: block;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a,.dropdown button, .dropdown-content a{
  cursor: pointer;
}

.topnav a span, .topnav button span{
  color:#a2b4c8;
  margin-right: 10px;
  text-shadow: 1px 1px 2px rgba(22,24,29,1);
  margin-left: 11px;
}

.dropdown-content a span{
  margin-left: 17px;
}

.active {

}

.topnav .icon {
  display: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 17px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #555;
  color: white;
}

.dropdown-content a:hover {
  background-color: #ddd;
  color: black;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.body{
  margin-top: 1%;
  height:350px;
}

.body h1{
  color:#fff;
  text-align: center;
  margin: 40px 10px 10px 10px;
}

@media screen and (max-width: 700px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 700px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

Output Screenshot :

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *