CSS Child vs Descendant selectors

前端 未结 8 2209
心在旅途
心在旅途 2020-11-22 03:36

I am a bit confused between these 2 selectors.

Does the descendent selector:

div p

select all p withi

8条回答
  •  天命终不由人
    2020-11-22 03:55

    Bascailly, "a b" selects all b's inside a, while "a>b" selects b's what are only children to the a, it will not select b what is child of b what is child of a.

    This example illustrates the difference:

    div span{background:red}
    div>span{background:green}
    
    
    abcdefghi

    Background color of abc and def will be green, but ghi will have red background color.

    IMPORTANT: If you change order of the rules to:

    div>span{background:green}
    div span{background:red}
    

    All letters will have red background, because descendant selector selects child's too.

提交回复
热议问题