CSS select second level elements

后端 未结 3 829
天命终不由人
天命终不由人 2021-01-18 11:51

How to remove background on a second level li elements ?

  • Test
相关标签:
3条回答
  • 2021-01-18 12:04

    IT happens because you ars setting the background to the entire <li> , and the second level is inside to the first , your second level has a transparent background and that's the reason because you see red (is the inmediately background set). You have 2 options:

    1. set the background to the elements
    2. set a background matching to the original background

    I recommend set the background to the elements like this:

    ul.navi > li  {
        line-height: 36px;
        border-radius: 8px;
        margin-bottom: 10px;
    }
    ul.navi > li > a {
     background-color: red;
        }
    

    fiddle : http://jsfiddle.net/zhrgyLrf/2/

    0 讨论(0)
  • 2021-01-18 12:17

    Your li is inside the red li. Try to just set another color, for example

    ul.navi > li > ul > li {
        background: #fff;   
    }
    

    Color: transparent will also not work here... Because when You've got color: transparent, it is transparent, and the "below" red is visible underneath it.

    Good luck, hope it helps.

    Updated: http://jsfiddle.net/zhrgyLrf/1/

    0 讨论(0)
  • 2021-01-18 12:26

    Why not just set the background on the direct a child elements?

    Updated Example

    ul.navi > li > a {
        background-color: red;
    }
    

    The reason background: none wasn't working is because you are setting the background on the entire parent, li element. Thus, even though the children don't have a background, the parent's background is still shown because it encompasses the children. As an alternative, you would have to set the children's background to #fff. In doing so, you would unfortunately lose your transparency, though.

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