My navigation structure looks like this:
<div class="menu">
<ul>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page with Subpages</a>
<ul class="children">
<li><a href="#">Page with another subpage</a>
<ul class="children">
<li><a href="#">subsubpage</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Page 3</a></li>
<li><a href="#">Page 4</a></li>
</ul>
</div>
I want all <li>
's that have a subnavigation children to have a little down-arrow applied so users know this page has subpages.
Is it possible to detect in pure css if a <li>
has children of ul.children
? In my example above, the "Page with Subpages" should have the down arrow applied and the "Page with another subpage" as well.
Right now I'm using jquery to solve this problem:
$(function() {
$('.menu a').each(function() {
if ( $(this).parent('li').children('ul').size() > 0 ) {
$(this).append('<span class="dwn">▼</span>');
}
});
});
Is there an easy pure CSS way to do so?
Thank you.
You could do this:
here is an example
This doesn't have support in all browsers, here is a list of support
EDIT
Just realised that my example above will be flawed, the arrow will be in the wrong position. Still - if this is an avenue you would like to pursue the possibility is there to align the arrow correctly.
There is no CSS 'parent selector'.
But here is a shorter jquery code:
Your question is similar to this one:
Complex CSS selector for parent of active child
For Wordpress Please Use the Following
Thanks to https://www.billerickson.net/code/add-arrows-to-menu-items/
In the interests of keeping it simple, here is the solution I like to use:
Place tags around the name of the item acting as a drop-down menu.
Add the arrow using CSS:
Hope this works for you! I believe it to be the simplest method I've come across!
For example
and the CSS
I think the best way would be to add a class (or that span you're using) to those
li
s on the server.Another solution, but probably not easy and not clean, is to add the arrow to all
ul
s and only make them visible (using css) in ul.children. You can then try to position the arrow in front of or behind the parent li. Whether this will work will depend on the design.