0

I have an element that can contain multiple unordered lists.

I want to selectivly style the separators - that is the separators that divide LI elements and separators that divide UL elements. Here's some HTML:

What I'm effectively trying to achieve is:

  1. Everything is separated either by UL separator or LI separator
  2. If it's the last UL, no UL separator but last LI has a separator
  3. If it's not the last UL - the last LI has no separator

<div>
    <ul>
        <li></li><!-- has blue bottom border -->
        <li></li><!-- has blue bottom border -->
        <li></li><!-- HAS NO BOTTOM BORDER -->
    </ul><!-- has green bottom border -->
    <ul>
        <li></li><!-- has blue bottom border -->
        <li></li><!-- has blue bottom border -->
        <li></li><!-- has blue bottom border -->
    </ul><!-- HAS NO BOTTOM BORDER -->
</div>

I've come up with this code - which works but the last LI of the last UL dows not have a separator.

.nav-blue ul
{
    border-bottom:solid 0.5rem Green;
}

.nav-blue ul:last-child
{
    border-bottom:0;
}

.nav-blue li
{
    border-bottom:solid 0.1rem Blue;
}

.nav-blue li:last-child
{
    border-bottom:0;
}

/* This does not work */
.nav-blue not(ul:last-child) li:last-child
{
    border-bottom:solid 0.1rem Blue;
}

There may be also be prettier ways of achieving this and I'm keen for help and further advice.

Thanks in advance.

1
  • Can you not apply classes to the ul or li elements? Commented Jan 27, 2015 at 11:19

2 Answers 2

3

Using :not pseudoselector (in your example you forgot the :) you can write like so

/* 1 */ div li { 
            border-bottom: 1px blue solid; 
        }

/* 2 */ div ul:not(:last-child) { 
            border-bottom: 1px green solid; 
        }

/* 3 */ div ul:not(:last-child) li:last-child { 
            border-bottom: 0; 
        }

Basically with these rules the border

  1. is always set on li (blue)
  2. is always set on ul (green) except when it is the last-child
  3. is removed on every li:last-child except when it belongs to the last ul
Sign up to request clarification or add additional context in comments.

Comments

1

try this : http://jsfiddle.net/zj9v81t7/19/

and css code :

ul , li{margin:0; padding:0}
div ul
{
    border-bottom:1px solid Green;
    padding-bottom:10px; margin-bottom:10px;

}
div ul li
{
    border-bottom:1px solid blue;
}
div ul li:last-child{
    border-bottom:0;
}

div ul:last-child
{
    border-bottom:0;
}

div ul:last-child li
{
    border-bottom:1px solid blue;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.