0

I have nested list, similar to the one below:

<ol>
    <li>Item 1
        <ol>
            <li>Item 2</li>
            <li>Item 3
                <ol>
                    <li>Item 4
                        <ol>
                            <li>Item 5</li>
                        </ol>
                    </li>
                    <li>Item 6
                        <ol>
                            <li>Item 7</li>
                        </ol>
                    </li>
                </ol>
            </li>
        </ol>
    </li>
</ol>

Items 1, 5 and 7 are all lone <li> elements, i.e. they are the only ones in their level of the hierarchy for that specific parent. I would like to match them and exchange them for bullet elements. (I would also be open to a JavaScript solution, though I would prefer pure CSS, if possible.)

2
  • You mean, items 2, 5 and 7... right? (1 has a nested list) Commented Apr 3, 2015 at 11:52
  • No, I do mean items 1, 5 and 7. Item 2 and item 3 are on the same level with the same parent. Item 1 doesn't have another item on that level, even though it still has a nested list. Commented Apr 3, 2015 at 11:56

2 Answers 2

2

I think you can use the :only-child pseudo-class.

See reference: http://www.w3.org/TR/css3-selectors/#only-child-pseudo

The trick requires two CSS rules.

The ol li:only-child rule may cause issues due to inheritance.

Use li:only-child * to cancel any effect from the first rule.

Seems to work okay in this example.

ol li:only-child {
  font-weight: bold;
}
li:only-child * {
  font-weight: normal;
}
<ol>
    <li>Item 1
        <ol>
            <li>Item 2</li>
            <li>Item 3
                <ol>
                    <li>Item 4
                        <ol>
                            <li>Item 5</li>
                        </ol>
                    </li>
                    <li>Item 6
                        <ol>
                            <li>Item 7</li>
                        </ol>
                    </li>
                </ol>
            </li>
        </ol>
    </li>
</ol>

Sign up to request clarification or add additional context in comments.

2 Comments

Depending on the layout the OP may want to scope down the selectors to ol > li:only-child and li:only-child > ol so as not to block inheritance.
@BoltClock I like your suggestion, makes for a cleaner set of rules. Please feel free to edit/improve the answer if you have time to add a few words.
1

Here's a jquery based selector, if that's any use:

$('li').filter(function() { return $(this).siblings().length == 0 })

https://jsfiddle.net/5ge811g5/

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.