2

I would like to achieve a simple effect of enlarging font size and adding color borders and background in top-level li elements of a list, without influencing lower nested levels (sub-lists). These four questions and answers are related, but do not answer my questions below.

Target first level <li>s and not the nested <li>s
Style first level of menu only
Style only first level of unordered list
styling a nested list in CSS


Here is the minimal example:

ol.toplevel > li {
 font-size: 160%;
 margin-bottom: 0.5em;
 margin-top: 1.5em;
 padding-bottom: 0.2em;
 padding-top: 0.1em;
}

ol.toplevel div.topleveltitle {
 background-color: yellow;
 border-bottom: 1px solid red;
 border-top: 1px solid red;
 color: red;
}

ol.toplevel li li {
 /*For lower nested levels, we don't want to inherit any top-level settings*/
 all: revert; /*does not seem to work at all, "unset" neither*/
 font-size: 70%; /*undo 160%, but we don't want to propagate 70% to lower levels!*/
}
<ol class="toplevel">
    <li><div class="topleveltitle">First title</div></li>
    <li><div class="topleveltitle">Second title</div>
        <ol>
            <li>Subitem</li> 
            <li>Subitem</li> 
            <li>Subitem</li> 
            <li>Subitem</li> 
        </ol>
    </li>
    <li>Third title
        <ul>
            <li>Subitem
            <ol>
                 <li>Subsub</li>
                 <li>Subsub</li>
            </ol>            
            </li> 
        </ul>
    </li>
</ol>

After so many years of css development, I would hope that this simple goal can be achieved in a simple, concise and elegant way – without tricks, hacks and redundancy. So here are the specific questions regarding this example:

  1. Is it possible to avoid introducing helper <div> or <span> along with topleveltitle, and use css to only define style for the top-level <ol> to affect each top-level <li> text without influencing the contents of nested sub-lists? It would work if the </li> closing tag was placed immediately after the titles, but then it would be invalid html because, per html specs, the closing tag must be placed after nested sub-lists as in the example.
  2. Is it possible to extend, in a possibly simple way, the color background and borders to the list item number?
  3. Is it possible to avoid all this complexity of preventing inheritance of styles from top level to lower levels of nesting? "revert" does not work as used, and I don't want to explicitly enumerate and set all the properties modified in top-level for lower levels, especially given that their values may be unknown because they themselves may be inherited or dependent on user/browser settings. I would rather want to affect only toplevel instead of affecting all levels and then enumerating exceptions or redefining values for lower levels.
dolphin
  • 1,192
  • 10
  • 27
  • note that `revert` work only on Fiferox for now – Temani Afif Nov 23 '19 at 23:25
  • @TemaniAfif I did test it on Firefox only. – dolphin Nov 23 '19 at 23:48
  • have you considered making them siblings to another `li` that contains the children? Alternatively, it's hard to say because I don't know the content, but depending on the semantics [`
    `](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dd) may be a good fit
    – soulshined Nov 23 '19 at 23:52
  • @soulshined ``
    `` and ``
    `` are not suitable for this use-case... The current approach with nested lists is perfectly suitable (and I have a huge complicated list, this is just a minimal example), I just have a problem with very simple styling as described. Regarding "making them siblings to another ``li`` that contains the children", I don't understand what should be made siblings, but this sounds to me like creating artificial/redundant structure/items in order to help with the css.
    – dolphin Nov 25 '19 at 03:04
  • html is inherently redundant so what's the burden of typing 1 more simple tag if it's semantically correct and provides a solution, something that answers all of your questions? – soulshined Nov 25 '19 at 03:30
  • @soulshined html is inherently redundant? only in syntax, and even that, given its assumptions, is not really redundant unless you are counting characters. "what's the burden of typing 1 more simple tag" – it is just not right to spoil the proper structure by introducing unnecessary elements just to make it easier to make it "look better" by using convoluted rules relying on the existence of those unnecessary elements. Also, in the real usage scenario you have a structure with a hundred nested items and many nesting levels. – dolphin Nov 27 '19 at 16:54
  • 1
    "not right to spoil the proper structure"...being semantically correct is proper structure; I don't think we are on the same page. The solution I recommended is commonly used for things akin to 'pure' CSS drop-down menus. If you don't think it fits your needs that's fine, too each their own. I was just elaborating so future readers understand there are alternatives that address this in a semantic-friendly way, with little effort, in lieu of javascript. cheers – soulshined Nov 27 '19 at 17:08
  • @soulshined I appreciate the idea you mentioned, but is introducing redundant items that do not exist in the actual data, and will be hidden because end users must not see them, "semantically correct"? I am surprised you see it this way, because (cite) "Semantically correct usage of elements means that you use them for what they are meant to be used for". And I think ``
  • `` was not meant to be used to introduce redundant empty invisible items. One could argue that it could be even worse if we used something else instead of ``
  • ``, but to call it "correct"...
  • – dolphin Nov 29 '19 at 05:50
  • It is semantically correct. In fact, I would even argue it can be more semantically favored because li [permitted content](https://html.spec.whatwg.org/multipage/grouping-content.html#the-li-element) is flow content (which includes h1,h2 etc) How is it empty or invisible if it contains the `topleveltitle` content? I never implied empty/redundant, these were your assumptions which is why im certain we’re not on the same page. I dont know how I got you there I just asked if you considered making them siblings to the children. Its still a correct way to do it; Im not saying its *the* way – soulshined Nov 29 '19 at 06:43
  • @soulshined This is why I said in my first answer "I don't understand what should be made siblings, but this sounds to me like creating artificial/redundant structure/items in order to help with the css". You didn't deny that and answered "html is inherently redundant so what's the burden of typing 1 more simple tag" – like "html is already a mess, what's the problem with adding a bit more". Since in my example code the number of ``
  • ``, ``
      `` and ``
      `` and the structure is sufficient and just right, please explain your siblings idea and how adding more tags is not redundant...
  • – dolphin Nov 29 '19 at 12:25