0

如何制定将执行以下操作的规则:

.container .unit:first-child(如果里面有 div.box1.extra) + .box2 { top: 50px;}

<div class="container">
   <div class="unit">
       <div class="box1 extra"><!-- content --></div>
   </div>

   <div class="unit">
       <div class="box1"><!-- content --></div>
   </div>

   <div class="box2"><!-- content --></div>                 
</div>
4

1 回答 1

0

This is unfortunately not possible with CSS at present. What you are referring to is DOM traversal with CSS. What you can do is simply declare the rule anyway.

.container .unit:first-child .box1.extra + .box2 { top: 50px;}

if the .box2 element does not exist, nothing will change. for the units that do have a .box2, the rule will apply.

EDIT: Note that this only works if .box2 is a child of .unit. If .unit and .box2 are siblings, this unfortunately impossible.

于 2013-01-22T20:13:49.367 回答