Step 1:
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:
background-color: color | transparent;
Property Values:
.navbar-nav > .active > a { background-color: color ; }
<!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= "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> <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>