CSS Navbar

What is a CSS Navigation Bar?

A CSS navigation bar, or navbar, is a key interface element in web design that provides navigation links or menus to help users browse different sections of a website. It's a visual guide that enhances user experience by allowing easy access to various parts of the site.

Characteristics of CSS Navigation Bar

Layout Options: Navigation bars can be positioned either horizontally across the top or vertically along the side of a web page.

Navigation Links: The menu contains links to various pages and sections of the site. These links can be styled as buttons, text, or icons.

Dropdown Menus: Navigation bars can include dropdown menus that display additional links or options when a user hovers over or clicks on a menu item.

Styling: CSS allows designers to customize the visual elements of the navigation bar, including colors, fonts, borders, and hover effects, to create a cohesive and visually appealing design.

Responsive Design: Modern navigation bars are often designed to be responsive, meaning they adapt to different screen sizes and devices, ensuring usability on both desktop and mobile platforms.

Interactive Effects: CSS can add interactive effects to navigation elements, such as changing the link color on hover or click, or highlighting the current page.

Example:

<!DOCTYPE html>
<html>
<head>

  <title>CSS Navigation Bar Example</title>

  <style>

    /* Container for the navigation bar */
    .navbar {
      display: flex;
      background-color: #333;
      overflow: hidden;
    }

    /* Links inside the navigation bar */
    .navbar a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 20px;
      text-decoration: none;
      font-size: 17px;
    }

    /* Change the color of links on hover */
    .navbar a:hover {
      background-color: #ddd;
      color: black;
    }

    /* Add an active class to highlight the current page */
    .navbar a.active {
      background-color: #04AA6D;
      color: white;
    }
  </style>

</head>

<body>


  <!-- Navigation bar -->
  <div class="navbar">
    <a class="active" href="#home">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
  </div>

</body>
</html>

Output:

CSS Navigation Bar Variants

Here are examples of various types of navigation bars using CSS, including horizontal, dropdown, sticky, fixed, and full-height fixed vertical navbars.

Vertical Navigation Bar

A vertical navigation bar is positioned along the side of the page.

Example:

<!DOCTYPE html>
<html>
<head>

  <title>Vertical Navigation Bar</title>

  <style>
    .sidenav {
      height: 100%;
      width: 200px;
      position: fixed;
      top: 0;
      left: 0;
      background-color: #333;
      padding-top: 20px;
    }

    .sidenav a {
      padding: 15px 25px;
      text-decoration: none;
      font-size: 20px;
      color: #f2f2f2;
      display: block;
    }

    .sidenav a:hover {
      background-color: #575757;
      color: white;
    }
  </style>

</head>

<body>

  <div class="sidenav">
    <a href="#home">Home</a>
    <a href="#services">Services</a>
    <a href="#clients">Clients</a>
    <a href="#contact">Contact</a>
  </div>

</body>
</html>