Problem:
I'm making a nav CSS module. I'm wanting submenus to have a different background color than their parent menu. However, if there is a submenu within a submenu, I need that submenu to have the same background color as it's parent's parent. I know I could do this fairly verbosely if I set a limit on the number of submenus that would be allowed but I would prefer the option to have as many nested menus as needed and have them colored appropriately. Is there some CSS trickery that can do this or am I relegated to JS for this?
I've looked into :nth-child
and :nth-of-type
but these only apply to sibling elements within a parent node as far as I know.
Code:
ul.nav {
list-style: none;
}
ul.nav.dropdown {
height: 0;
overflow: hidden;
}
ul.nav.dropdown.show {
height: auto;
}
ul.nav li {
background-color: #44499f;
}
ul.nav ul li {
background-color: #b6ff00;
}
<ul class="nav">
<li><a>Test link</a></li>
<li><a>Test link</a></li>
<li><a>Test link</a></li>
<li>
<a class="dropdown-toggle">Test dropdown</a>
<ul class="nav dropdown">
<li><a>Test link</a></li>
<li><a>Test link</a></li>
<li><a>Test link</a></li>
<li>
<a class="dropdown-toggle">Test dropdown</a>
<ul class="nav dropdown">
<li><a>Test link</a></li>
<li><a>Test link</a></li>
<li><a>Test link</a></li>
</ul>
</li>
</ul>
</li>
</ul>