Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to change the background color of the active nav-item?

  • Last Updated : 03 Aug, 2021

Given an HTML document containing a list of items and the task is to change the background color of list of items when the item is active. The state of list of items can be changed by changing the background-color CSS property.

Syntax: 

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

background-color: color | transparent; 

Property Values:

  • color: It specifies the background color of element. 
  • transparent: It specifies that the background color should be transparent.

Hence, the background color of an active nav-item can be changed in the following manner by changing the background-color CSS property.



Syntax : 

.navbar-nav > .active > a { 
    background-color: color ; 
}

Example:

html




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="utf-8">
  
    <title>
        How to change the background 
        color of the active nav-item?
    </title>
</head>
  
<body>
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
  
    <style>
        .nav-link {
            color: green;
        }
  
        .nav-item>a:hover {
            color: green;
        }
  
        /*code to change background color*/
        .navbar-nav>.active>a {
            background-color: #C0C0C0;
            color: green;
        }
    </style>
    <ul class="navbar-nav">
        <li class="nav-item active">
            <a class="nav-link" href="#">
                Home 
                <span class="sr-only">
                    (current)
                </span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#">
                GeeksForGeeks Interview Prep
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#">
                GeeksForGeeks Courses
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link disabled"
                href="#">Disabled</a>
        </li>
    </ul>
  
    <script>
        $(document).ready(function () {
  
            $('ul.navbar-nav > li')
                    .click(function (e) {
                $('ul.navbar-nav > li')
                    .removeClass('active');
                $(this).addClass('active');
            });
        });
    </script>
</body>
  
</html>

Output:

HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.




My Personal Notes arrow_drop_up
Recommended Articles
Page :