applying css for only parent but not to children

前端 未结 4 1656
闹比i
闹比i 2020-12-05 09:58

I have an unordered list like this:

  • list1
    • Sub list1<
相关标签:
4条回答
  • 2020-12-05 10:06

    Use the direct descendant operator > for that:

    .list > ul > li { ... }
    

    The > operator selects only elements that are direct children of the element(s) before it.

    Note however, anything inside that list item will of course have the background of the list item despite not having any background color directly assigned to it.

    0 讨论(0)
  • 2020-12-05 10:15

    Alternatively, you can now do:

    li:not(li li) { background: #ccc; }
    
    0 讨论(0)
  • 2020-12-05 10:17

    You can use the > operator for that, like this:

    .list > ul > li{
      background:#ccc; 
    }
    
    0 讨论(0)
  • 2020-12-05 10:24

    The only way of doing this is like:

    .List > ul > li { background-color: #ccc; }
    

    If you want to apply only the bgcolor to the ul simply do:

    .List > ul { background-color: #ccc; }
    

    :)

    0 讨论(0)
提交回复
热议问题