Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSS selector that matches parent of empty element

CSS selector that matches parent of empty element

Problem

I have such HTML code

  • ...
  • I want to make selector for elements i in div.subMenuActions which will check if ul.subMenu is empty.

    I know that I can check if ul.subMenu is empty by using CSS selector ul.subMenu:empty, but I need to hide i.icon-caret-down, i.icon-caret-right if ul.subMenu is empty.

    Please no JS answers.

    Problem courtesy of: Epsil0neR

    Solution

    There is no way to select previous element sibling nor parent element with CSS on levels 1-3. As Mishik say, you can only select next siblings.

    In your case if you reorder elements in HTML and you know the height of the ul.submenu element, you can use this solution:

  • ...
  • then apply this css:

    li {
        position:relative;
        padding-bottom:2em;
    }
    ul.subMenu {
        height:2em;
        position:absolute;
        width:100%;
        bottom:0;
    }
    div.subMenuActions {
        position:relative;
    }
    ul, div { /* normalize to zero */
        margin:0;
        padding:0;
    }
    /* and the selectors */
    ul.subMenu:empty + .subMenuActions > i,
    ul.subMenu:empty + .subMenuActions + .subMenuActions > i {
        display: none;
    }
    

    http://jsfiddle.net/7VmR9/15/

    All in all, much easier way here to go is to use javascript.

    Solution courtesy of: Stano

    Discussion

    View additional discussion.



    This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

    Share the post

    CSS selector that matches parent of empty element

    ×

    Subscribe to Css3 Recipes - The Solution To All Your Style Problems

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×