简体   繁体   English

CSS选择器以选择父级层次结构中的子级

[英]CSS selector to select a child in the parent's hierarchy

Using css selectors I want to set the background of the button as red if it is the only button present in the hierarchy of .parent class. 如果使用CSS选择器,则我想将按钮的背景设置为红色(如果它是.parent类层次结构中唯一的按钮)。

I tried the below selector but it fails if the sibling of the button is inside a div. 我尝试了下面的选择器,但是如果按钮的兄弟位于div内则失败。

<style> 
.parent  .test:only-of-type {
    background: red;
}
</style>

Case1: no button should be marked red (selector fails in this case) 情况1:没有按钮应标记为红色 (在这种情况下选择器失败)

 <div class="parent">
      <div class="boat">
        <button class="test">This is a paragraph.</button>
      </div>
      <button class="test">This is a paragraph.</button>
     <button class="test">This is a paragraph.</button>
      <div class="other">
        <button class="test">This is a paragraph.</button>
      </div>
    </div>
 </div>

Case2: button should be marked red 情况2:按钮应标记为红色

 <div class="parent">
      <div class="boat">
        <button class="test">This is a paragraph.</button>
      </div>
   </div>
 </div>

Case3: no button should be marked red (selector fails in this case) 情况3:不应将任何按钮标记为红色 (在这种情况下选择器将失败)

 <div class="parent">
      <div class="boat">
        <button class="test">This is a paragraph.</button>
      </div>
      <div class="other">
        <button class="test">This is a paragraph.</button>
      </div>
    </div>
 </div>

To get the button red inside of the boat div you can use the selector: 要使Boat div内部的按钮变为红色,可以使用选择器:

.parent > .boat > .test {
}

You could use the :only-child selector: 您可以使用:only-child选择器:

 .parent > .boat:only-child > .test:only-child, .parent > .test:only-child { color: red; } 
 <div class="parent"> <div class="boat"> <button class="test">not red.</button> </div> <div class="other"> <button class="test">not red.</button> </div> </div> <div class="parent"> <div class="boat"> <button class="test">not red.</button> </div> <button class="test">not red.</button> <button class="test">not red.</button> <div class="other"> <button class="test">not red.</button> </div> </div> <div class="parent"> <div class="boat"> <button class="test">red.</button> </div> </div> 

Update 更新

Selector explained: 选择器说明:

  • .parent - start with your parent .parent从您的父母开始
  • > .boat:only-child greater than ( > ) means direct child so this means that .boat has to be a direct child of .parent and the only child (this is the :only-child bit) > .boat:only-child大于( > )表示直接子代,因此这意味着.boat必须是.boat的直接.parent且是唯一子代(这是:only-child位)
  • > .test:only-child again this means that .test has to be a direct child of .boat and the only child > .test:only-child再次是> .test:only-child ,这意味着.test必须是.boat的直接子代,并且是唯一子代

The second selector is optional - in case you just have a button without the boat div 第二个选择器是可选的-如果您只有一个按钮而没有Boat div

More info on only-child 有关独生子女的更多信息

More info on direct child 有关直子的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM