How to Create Responsive Navbar in Html and CSS: Complete Code


A responsive navbar is a crucial element for any website design as it improves user experience by providing easy and quick access to various website pages. By creating a navigation bar using HTML and CSS, you can ensure that your website is mobile-friendly, accessible, and optimized for all devices. 

In this blog post, I'll tell you how to build a horizontal responsive navbar using HTML and CSS, with complete code. 

You can modify this code according to your requirements.

So, let's get started!

HTML Code:

<!DOCTYPE html>
<html>
<head>
<title>Responsive Navbar</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
</head>
<body>
<nav>
<div class="logo">
<a href="#">Logo</a>
</div>
<div class="menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<div class="toggle">
<i class="fas fa-bars"></i>
</div>
</nav>
<script src="script.js"></script>
</body>
</html>

CSS Code:

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #333;
  color: #fff;
  padding: 10px;
}

.logo a {
  font-size: 24px;
  font-weight: bold;
  text-transform: uppercase;
  color: #fff;
  text-decoration: none;
}

.menu ul {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.menu li {
  margin: 0 10px;
}

.menu a {
  color: #fff;
  text-decoration: none;
}

.toggle {
  display: none;
  cursor: pointer;
  font-size: 24px;
}

@media(max-width: 768px) {
  .menu {
    display: none;
  }
  .toggle {
    display: block;
  }
  .menu.is-active {
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 60px;
    left: 0;
    width: 100%;
    background-color: #333;
    padding: 10px 0;
    z-index: 1;
  }
  .menu.is-active li {
    margin: 10px 0;
  }
}

Output:

How to Create Responsive Navbar in Html and CSS: Complete Code


This is just a basic navigation bar using HTML and CSS. You can modify this code according to your requirements.