Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CSS way to add an arrow if a ul has a ul child?

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.

like image 704
matt Avatar asked Mar 12 '11 08:03

matt


People also ask

How do you make an arrow symbol in CSS?

Use \25bc instead. And if you want to change the color just add css color property. i used "\25bc" , i got arrow icon.

How do I make a drop down arrow in HTML?

You can use pointer-events: none; so that the dropdown shows dropdown when you click on the arrow.

How do you make an arrow in HTML and CSS?

Approach: Creating an arrow using CSS is very simple. First, create an L(alphabet) shape using some box-shadow property and then rotate it to some angle to align them (both left and right arrows) together. HTML Code: In this section, two div elements are created, one for each arrow.


1 Answers

This is perfectly doable in CSS --

Your structure is this:

 <ul class="menu">       <li>            <a href="#">Has arrow</a>            <ul><li><a href="#"></a></li></ul>       </li>       <li>            <a href="#">Has no arrow</a>       </li>   </ul> 

Your CSS will be like this --

   //this adds an arrow to every link   .menu li > a:after { content: '>'; }     // this removes the arrow when the link is the only child   .menu li > a:only-child:after { content: ''; }    

Taking it one step farther, if you want to have a drop down menu where there is a down arrow on the top level items and then right arrows for sub menus, your CSS would look like this

   // set up the right arrows first    .menu li > a:after { content: '>'; }     //set up the downward arrow for top level items    .menu > li > a:after {content: 'v'; }     //clear the content if a is only child    .menu li > a:only-child:after {content: ''; } 

Here is a jsfiddle of it in action - http://jsfiddle.net/w9xnv/2/

like image 164
David Avatar answered Sep 21 '22 00:09

David