What is the difference between '>' and a space in CSS selectors?

前端 未结 5 656
野性不改
野性不改 2020-11-27 12:30

What\'s the point using this syntax

div.card > div.name

What\'s the difference between this

div.card div.name

5条回答
  •  死守一世寂寞
    2020-11-27 13:07

    > vs. Space

    Consider the two scenarios div > span { } vs. div span { }

    Here, the (space) selects all the all the elements inside

    element even if they are nested inside more than one element. The > selects all the children of
    element but if they are not inside another element.


    Take a look at two examples:

    > (Greater than):

    div > span {
      color: #FFBA00 ;
    }
    
      
    Hello...

    Hello!

    World!

    This one just selects the Hello... (because it's immediately after the nested div tag), and World! and it won't look for the inside

    tag because it's not immediately after a div tag.

    Space

    div span {
      color: #0A0 ;
    }
    
      
    Hello...

    Hello!

    World!

    This one selects all the span tags, even if they are nested inside other tags.

提交回复
热议问题