1

I am trying to select the sub-menu item from a Wordpress default sidebar menu, and I am trying to write a CSS selector for that. I thought I understand CSS selectors, but I don't know what is happening in this case.

The following ones are working:

.widget_nav_menu ul .menu-item .sub-menu { background: red; }
.widget_nav_menu ul .menu-item li { background: red; }

While this one doesn't work:

.widget_nav_menu ul .menu-item li .sub-menu { background: red; }

Can someone explain to me why can I not specify things to be more precise with both specifying class and type here?

Luckily at this level of customization I don't need to select things more precisely (I only want to hide sub-menu items), but can someone tell me how to make the non-working example work?

Here is a live site, but it’s the same on all Wordpress installs with TwentyTen theme and a multi-level menu on the left.

UPDATE: I think I got a big misunderstanding about the usage of spaces in CSS, so I asked a question here: usage of spaces in CSS files

BTW, after understanding the answers and realising what was wrong with my problem, the correct answer for my problem is:

.widget_nav_menu li.menu-item ul.sub-menu
0

3 Answers 3

2

try:

.widget_nav_menu ul .menu-item .sub-menu li { background: red; }

because sub-menu class belongs to ul and there is no sub-menu class after li

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

1 Comment

Thanks it works. But do I understand it right that if there is something like <ul class="sub-menu"> then I have to use either ul or .sub-menu in CSS, not both?
1

I believe you want this:

.widget_nav_menu ul .menu-item li.sub-menu { background: red; }

1 Comment

this shouldn't work, because sub-menu class belongs to ul and there is no li with sub-menu class
0

Try this :

.widget_nav_menu ul .menu-item .sub-menu li { background: red; }

Because the li item is below the .sub-menu ul...

2 Comments

Amazing, almost every word is the same as harakiri's answer.
I know, when I finished writing my answer (2 minutes after him), I saw he had the same answer ! :-S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.