Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dropdown arrow indicators to menu items that have submenus only?

I am currently trying to add arrow indicators on my navigation menu for items which have submenu options.

Currently I am using this CSS:

.mainNav li > a:after {
   color: #444;
   content: ' ▾';
}

But this adds a dropdown arrow to every <li> regardless of if there is a submenu or not. Is there a way with just CSS to only add this arrow to items that have sub-items?

Thanks!

like image 433
user3101431 Avatar asked Jan 17 '14 16:01

user3101431


People also ask

How do I add an arrow to a drop down menu?

The simplest way to get an arrow to show next to a select/dropdown option is to just add one in the text using a HTML entity or Unicode character. Here's a list of arrows you can use: HTML Arrows. I'd suggest → ( &rarr; ) or similar (not sure which direction you want!)

How do I create a dropdown menu with submenus in HTML and CSS?

Create A SubnavUse any element to open the subnav/dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the subnav menu and add the subnav links inside it. Wrap a <div> element around the button and the <div> to position the subnav menu correctly with CSS.


1 Answers

No. CSS has no contains child selector. You'd probably be better to just add a class to the li element. For example:

<li class="has-child">
    <a href="#">The Link</a>
    <ul class="child">
        <li>Child 1</li>
    </ul>
</li>

Your CSS selector would in turn look like:

.mainNav li.has-child > a:after {
   color: #444;
   content: ' ▾';
}

You could have jQuery add the class for you, if that's an option:

$('.mainNav li:has(ul)').addClass('has-child');

jsFiddle Demo

like image 147
BenM Avatar answered Sep 24 '22 00:09

BenM