0

I constantly fail to prevent a submenu item from inheriting the color of a top item. I know that similar questions have been asked already but am sorry to not be able to solve my problem using them. CSS:

.Topmenu a{
  color:black;}
.Topmenu.update a{
  color:blue;}
.Submenu a{
  color:black;}

Now the Menu itself looks like this:

<div id='Mainmenu'><ul>
<li class='Topmenu update'><a href='Link1'>Link1</a><ul>
<li class='Submenu'><a href='Link2'>Link2</a></li>
</ul></li>

Now the submenu Link still is blue. What am I doing wrong?

Edit: Sorry for the confusion, it's a 2D-Menu, the Main Menu(Topmenu items) is horizontal with vertical Topmenu's (Submenu Item).

1
  • 1
    At first you never close your first <li> there is an <ul> behind your first list item. Commented Aug 25, 2017 at 8:50

3 Answers 3

1

It is because of specificity. The .Submenu class is overwritten by .Topmenu.update. To avoid this, put .Topmenu.update in front of the .Submenu class.

.Topmenu a {
  color: black;
}

.Topmenu.update a {
  color: blue;
}

.Topmenu.update .Submenu a {
  color: black;
}
<div id='Mainmenu'>
  <ul>
    <li class='Topmenu update'><a href='Link1'>Link1</a>
      <ul>
        <li class='Submenu'><a href='Link2'>Link2</a></li>
      </ul>
    </li>
  </ul>
</div>

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

1 Comment

Thank you for your help! I didn't know that .Topmenu.update .Submenu a has to be specified differently!
0

The problem is from your css. it should look like this;

.Topmenu a {
  color: black;
}
.Topmenu, .update a {
  color: blue;
}
.Submenu a {
  color: black;
}

Notice the second block has a comma and space after the first class.

Although your HTML does have an error, it should still not affect the CSS from working right. and yes you have to fix your html it does not look right. so fix from @zowie's correction.

Let me know.

1 Comment

I just tried it, if i put </li> after link1, the following ul doesnt contain to that submenu, resulting in a breakdown of the whole menu. the <ul> must be contained in the topmenu- li to be a submenu.
0

Use > for the first level:

.Topmenu a{
    color:black;
}
.Topmenu.update > a{
  color:blue;
}
.Submenu a{
  color:black;
}

Comments

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.