.class>之间的区别.class和.class .class

时间:2014-03-15 05:33:28

标签: css css-selectors

我只是想知道:

之间的区别
.class .class{
font-size:14px;
}

VS

.class > .class{
font-size:14px;
}

是一回事吗?

1 个答案:

答案 0 :(得分:15)

不,它们不一样 - 第一个例子是descendant selector,第二个例子是direct child selector


.class .class将定位具有类.class的所有元素,该类派生自具有类.class的任何元素,例如

<div class="class">
 <div class="other">
    <div class="class"> This is targeted. </div>
 </div> 
</div>

jsFiddle example


.class > .class只定位具有类.class的元素的直接子元素,例如

<div class="class">
   <div class="other">
      <div class="class">This isn't targeted.</div>
   </div> 
   <div class="class">
      <div class="class">This is targeted, as it is a direct child.</div>
   </div>    
</div>

jsFiddle example.