Skip to content Skip to sidebar Skip to footer

Css- Ckeck And Add Right Arrow Where Li Has Ul

I want to add css right arrow where ul has a ul child? Below is triangle right css. I want to add in my menu so that users know this menu has sub menu. How is it possible to detect

Solution 1:

So there's really no parent selector in CSS, but as you've tagged this with jQuery, we can use the :has() selector to do much the same behavior. Basically, if a LI el contains a UL, we should assume the UL is a submenu, and I will add the 'js-has-a-submenu' class to the anchor el that we're using for the menu items. I've prefixed it with 'js-' simply to reflect that it isn't a statically applied class, but one we'll only use via js.

The code here is actually taken from the menu example I'd referenced in the commments, but then I noticed they'd hard-coded the down-arrow into the menu, which really breaks what you're trying to do, from what I understand. So I removed the down arrow, and used jQuery to apply the class. To understand the menus themselves, please do go read this, as I think it does pretty well (not a fan of the pink, but its css).

$("li:has(ul)").find("a:eq(0)").addClass("js-has-a-submenu");
body {
  background: #333;
}

.clearfix:after {
  display: block;
  clear: both;
}


/*----- Menu Outline -----*/.menu-wrap {
  width: 100%;
  box-shadow: 0px1px3pxrgba(0, 0, 0, 0.2);
  background: #3e3436;
}

.menu {
  width: 1000px;
  margin: 0px auto;
}

.menuli {
  margin: 0px;
  list-style: none;
  font-family: 'Ek Mukta';
}

.menua {
  text-decoration:none;
  transition: all linear 0.15s;
  color: #919191;
}

.menuli:hover > a,
.menu.current-item > a {
  text-decoration: none;
  color: #be5b70;
}

.menu.arrow {
  font-size: 11px;
  line-height: 0%;
}


/*----- Top Level -----*/.menu > ul > li {
  float: left;
  display: inline-block;
  position: relative;
  font-size: 19px;
}

.menu > ul > li > a {
  padding: 10px40px;
  display: inline-block;
  text-shadow: 0px1px0pxrgba(0, 0, 0, 0.4);
}

.menu > ul > li:hover > a,
.menu > ul > .current-item > a {
  background: #2e2728;
}


/*----- Bottom Level -----*/.menuli:hover.sub-menu {
  z-index: 1;
  opacity: 1;
}

.sub-menu {
  width: 160%;
  padding: 5px0px;
  position: absolute;
  top: 100%;
  left: 0px;
  z-index: -1;
  opacity: 0;
  transition: opacity linear 0.15s;
  box-shadow: 0px2px3pxrgba(0, 0, 0, 0.2);
  background: #2e2728;
}

.sub-menuli {
  display: block;
  font-size: 16px;
}

.sub-menulia {
  padding: 10px30px;
  display: block;
}

.sub-menulia:hover,
.sub-menu.current-itema {
  background: #3e3436;
}

.js-has-a-submenu::after {
     content: " \2193";
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="menu-wrap"><navclass="menu"><ulclass="clearfix"><li><ahref="#">Home</a></li><li><ahref="#">Movies </a><ulclass="sub-menu"><li><ahref="#">In Cinemas Now</a></li><li><ahref="#">Coming Soon</a></li><li><ahref="#">On DVD/Blu-ray</a></li><li><ahref="#">Showtimes &amp; Tickets</a></li></ul></li><li><ahref="#">T.V. Shows</a></li><li><ahref="#">Photos</a></li><li><ahref="#">Site Help</a></li></ul></nav></div>

Solution 2:

What if you would not hide the ul, but the li? Then you could use ::before to get css to insert a little arrow before your ul.

Please have a look at my fiddle https://jsfiddle.net/dL758uba/

/* Menu Styles */ul {display: inline;}

ul.third-level-menu::before {
content: '-';
position: relative;
left: -200px;
top: 10px;
}

ul::before {
content: '-';
position: relative;
left: 0px;
top: -20px;
}

.third-level-menu
{
    position: absolute;
    top: 0;
    right: -220px;
    width: 220px;
    list-style: none;
    padding: 0;
    margin: 0;

}

.third-level-menu > li
{
    height: 30px;
    background: #999999;
    display: none;
}
.third-level-menu > li:hover { background: #CCCCCC; }

.second-level-menu
{
    position: absolute;
    top: 30px;
    left: 0;
    width: 200px;
    list-style: none;
    padding: 0;
    margin: 0;

}

.second-level-menu > li
{
    position: relative;
    height: 30px;
    background: #999999;
    display: none;
}
.second-level-menu > li:hover { background: #CCCCCC; }

.top-level-menu
{
    list-style: none;
    padding: 0;
    margin: 0;
}

.top-level-menu > li
{
    position: relative;
    float: left;
    height: 30px;
    width: 100px;
    background: #999999;
}
.top-level-menu > li:hover { background: #CCCCCC; }

.top-level-menuli:hover > ul >li
{
    /* On hover, display the next level's menu */display: block;
}


/* Menu Link Styles */.top-level-menua/* Apply to all links inside the multi-level menu */
{
    font: bold 14px Arial, Helvetica, sans-serif;
    color: #FFFFFF;
    text-decoration: none;
    padding: 00010px;

    /* Make the link cover the entire list item-container */display: block;
    line-height: 30px;
}
.top-level-menua:hover { color: #000000; }

It is a bit rough, but I think it will help you in the right direction.

Post a Comment for "Css- Ckeck And Add Right Arrow Where Li Has Ul"