Open In App

How to make Responsive Navbar Menu in CSS ?

A responsive navbar menu is a crucial element of a website that adjusts its layout and appearance based on the screen size and device type used by the user. This adaptability ensures that users can easily navigate the website and access its various features regardless of whether they are using a desktop, tablet, or mobile device.

Approach

Example: Implementation of creating a responsive navbar menu in CSS.

<!DOCTYPE html>
<html lang="en" dir="ltr">

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

<body>
    <nav>
        <input type="checkbox" id="check">
        <label for="check" class="checkbtn">
            <i class="fas fa-bars"></i>
        </label>
        <label class="logo">GFG</label>
        <ul>
            <li><a class="active" href="#">HTML</a></li>
            <li><a href="#">Javascript</a></li>
            <li><a href="#">ReactJS</a></li>
            <li><a href="#">NodeJS</a></li>
        </ul>
    </nav>
</body>

</html>
/* style.css*/

body {
    margin: 0;
    padding: 0;
    font-family: Arial, sans-serif;
  }
  
  nav {
    background-color: green;
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
  }
  
  .logo {
    font-size: 1.5rem;
  }
  
  ul {
    display: flex;
    list-style: none;
    padding: 0;
    margin: 0;
  }
  
  ul li {
    margin-right: 20px;
  }
  
  ul li a {
    color: white;
    text-decoration: none;
    transition: color 0.3s;
  }
  
  ul li a:hover {
    color: lightgreen;
  }
  
  .checkbtn {
    font-size: 30px;
    color: white;
    cursor: pointer;
    display: none;
  }
  
  #check {
    display: none;
  }
  
  @media (max-width: 768px) {
    .checkbtn {
      display: block;
      order: 1;
      margin-right: 20px;
    }
  
    ul {
      position: fixed;
      top: 80px;
      right: -100%;
      background-color: green;
      width: 100%;
      height: 100vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      transition: all 0.3s;
    }
  
    ul li {
      margin: 20px 0;
    }
  
    ul li a {
      font-size: 20px;
    }
  
    #check:checked ~ ul {
      right: 0;
    }
  }

Output:

gfgnav

Article Tags :