2

How to add background color to the active menu item with css (ie when I click on about link and go to about page I want that active menu item have red background)?

This is my code in Wordpress:

<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation"> <ul class="nav navbar-nav"> <?php wp_nav_menu( array( 'theme_location' => 'main-menu', 'menu_class' => 'nav navbar-nav' ) ); ?> </ul> </nav> 

and this is my css:

.navbar-inverse .navbar-nav > li > a:visited { background: red !important; } 

But this css targets and "hover elements. What am I doing wrong?

1
  • 1
    Well, I think WordPress CMS toggles automatically a active class or something similar to that. So you can target only that one class. It would be nice if you could provide a snippet of already generated navigation bar HTML. Commented Nov 16, 2014 at 11:51

2 Answers 2

1

By default, WordPress menus output this class with the list item: current-menu-item so you can use this method to target active menu items:

li.current-menu-item { background: $sec-color; } 

If your theme supports the creation of multiple navigation menus, then you’ll need to be a bit more specific in your CSS

#menu-main-pages li.current-menu-item { background: #3FAEA5; color:#fff; } 

source: http://premium.wpmudev.org/blog/how-to-highlight-the-current-page-when-using-wordpress-navigation-menus/

Sign up to request clarification or add additional context in comments.

1 Comment

How to change the color wher hover over the navbar item ?
0

I just looked at some WordPress sites, so there are several options you could try, because I don't know which class your WordPress installation triggers if a menu item is active.

Option 1:

.navbar-inverse .navbar-nav > li.current-page-item > a{ background: red; } 

Option 2:

.navbar-inverse .navbar-nav > li.current-menu-item > a{ background: red; } 

Option 3:

.navbar-inverse .navbar-nav > li a.current { background: red; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.