How To Change Navigation bar Color in Bootstrap

admin_img Posted By Bajarangi soft , Posted On 08-10-2020

A navigation bar is a navigation header that is placed at the top of the page,With Bootstrap, a navigation bar can extend or collapse, depending on the screen size, standard navigation bar is created with the .navbar class, followed by a responsive collapsing class: .navbar-expand-xl|lg|md|sm (stacks the navbar vertically on extra large, large, medium or small screens),To add links inside the navbar, use a <ul> element with class="navbar-nav". Then add <li> elements with a .nav-item class followed by an <a> element with a .nav-link class

Navigation bar

The navigation bar color can be changed in Bootstrap using 2 methods:
Method 1: Using the inbuilt color classes
Changing the text color
The text color of the navigation bar can be changed using two inbuilt classes:

  • navbar-light: This class will set the color of the text to dark. This is used when using a light background color.
  • navbar-dark: This class will set the color of the text to light. This is used when using a dark background color.

Bootstrap 4 has a few inbuilt classes for the colors of any background. These can be used to set the color of the background of the navigation bar. The various background classes available are:

  • .bg-primary: This sets the color to the primary color.
  • .bg-secondary: This sets the color to the secondary color.
  • .bg-success: This sets the color to the success color.
  • .bg-danger: This sets the color to the danger color.
  • .bg-warning: This sets the color to the warning color.
  • .bg-info: This sets the color to the info color.
  • .bg-light: This sets the color to the light color.
  • .bg-dark: This sets the color to the dark color.
  • .bg-white: This sets the color to the white color.
  • .bg-transparent: This sets the navbar to be transparent.
Example(1)
  
<head>
        <title>
          How to change navigation bar color in Bootstrap ?
      </title>
      
        <!-- Include Bootstrap CSS -->
        <link rel="stylesheet" href=
        "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
  
<body>
    <!-- Navbar text is dark and background is light -->
    <nav class="navbar navbar-light bg-light">
            <a class="navbar-brand" href="#">
              Light color background
          </a>
        </nav>
  
    <nav class="navbar navbar-light bg-warning">
            <a class="navbar-brand" href="#">
              Warning color background
          </a>
        </nav>
  
    <!-- Navbar text is light and background is dark -->
    <nav class="navbar navbar-dark bg-dark">
            <a class="navbar-brand" href="#">
              Dark color background
          </a>
        </nav>
  
    <nav class="navbar navbar-dark bg-primary">
            <a class="navbar-brand" href="#">
              Primary color background
          </a>
        </nav>
  
    <nav class="navbar navbar-dark bg-secondary">
            <a class="navbar-brand" href="#">
              Secondary color background
          </a>
        </nav>
  
    <nav class="navbar navbar-dark bg-success">
            <a class="navbar-brand" href="#">
              Success color background
          </a>
        </nav>
  
    <div class="container">
            <h1 style="color: green">GeeksforGeeks</h1>
            <b>
              How to change navigation bar color in Bootstrap ?
          </b>
            <p>The above navigation bars use some of the
              default color classes available in Bootstrap4.</p>
        </div>
</body>
  
</html>
 
Method 2: Creating a custom class for the navigation bar
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>How to change the background color of the active nav-item?</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<style>
    .nav-link {
        color: green;
    }

    .nav-item>a:hover {
        color: #3892ce;
    }

    /*code to change background color*/
    .navbar-nav>.active>a {
        background-color: #f3a1a1;
        color: white;
    }

    .navbar-custom {
        background-color: #21dea8;
    }
    .form-inline{
     margin-left:450px;
    }
</style>



<body>
<br>
<div class="container">
<nav class="navbar navbar-expand-sm bg-warning navbar-custom">
<ul class="navbar-nav">
    <li class="nav-item active">
        <a class="nav-link" href="#">
            Home
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="#">
          About Us
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="#">
            Contact US
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link disabled"
           href="#">Disabled</a>
    </li>
</ul>
    <br>
    <form class="form-inline" action="/action_page.php">
        <input class="form-control mr-sm-2" type="text" placeholder="Search">
        <button class="btn btn-success" type="submit">Search</button>
    </form>
</nav>
</div>
</body>
</html>

<script>
    $(document).ready(function () {

        $('ul.navbar-nav > li')
            .click(function (e) {
                $('ul.navbar-nav > li')
                    .removeClass('active');
                $(this).addClass('active');
            });
    });
</script>

 

Related Post