简体   繁体   English

CSS选择具有特定子元素的元素的兄弟姐妹?

[英]CSS Select Sibling of An Element with A Specific Sub-Element?

The snippet below is a part of a much larger structure with many .step elements.下面的代码片段是包含许多 .step 元素的更大结构的一部分。

I need to match all .stepText elements that are next to .stepTitleAndImages ul.standard我需要匹配所有.stepText旁边,是元素.stepTitleAndImages ul.standard

In other words, match all .stepText elements that have .step parent that has .stepTitleAndImages child that has .stepImages.standard child换句话说,匹配所有.stepText具有的元素.step有父.stepTitleAndImages孩子有.stepImages.standard孩子

<div class="step">
  <div class="stepTitleAndImages">
    <h3 class="stepTitle"></h3>
    <ul class="stepImages standard"></ul>
    <div class="clearer"></div>
  </div>
  <div class="stepText "></div>  **HOW TO SELECT ALL ELEMENTS LIKE THIS ONE?**
  <div class="clearer"></div>
</div>

<div class="step">
  <div class="stepTitleAndImages">
    <h3 class="stepTitle"></h3>
    <ul class="stepImages medium"></ul>
    <div class="clearer"></div>
  </div>
  <div class="stepText "></div>
  <div class="clearer"></div>
</div>

PS: I cannot modify the HTML. PS:我无法修改 HTML。 Cannot use anything other than pure CSS.不能使用纯 CSS 以外的任何东西。

Just use this for selecting first case只需使用它来选择第一种情况

.step:nth-child(1) .stepText {
... Your CSS here
}

For second one use第二次使用

.step:nth-child(2) .stepText {
... Your CSS here
}

For selecting both use选择两者使用

.step .stepText {
... Your CSS here
}

Then you should require jquery for that那么你应该为此需要jquery

Selecting Parents sibling is not possible only with pure CSS yet, You can achieve this by a single line of jquery:仅使用纯 CSS 还不能选择父级兄弟姐妹,您可以通过一行 jquery 来实现:

$('ul.standard').parent().siblings(".stepText").css(...your CSS here);

This cannot be done with your HTML structure and with pure CSS.不能用你的 HTML 结构和纯 CSS 来完成。 The closest solution to your problem, changing the HTML structure and with pure CSS, would be to move the standard class to its parent tag:最接近您的问题的解决方案是更改 HTML 结构并使用纯 CSS,将standard类移动到其父标签:

<div class="stepTitleAndImages standard"> <h3 class="stepTitle"></h3> <ul class="stepImages"></ul> <div class="clearer"></div> </div>

This would allow you to use the adjacent sibling selector ( + ), which matches the second selector if it's the direct next sibling of the first , like this:这将允许您使用相邻的兄弟选择器( + ),如果第二个选择器是第一个的直接下一个选择器,则它匹配第二个选择器,如下所示:

.stepTitleAndImages.standard + .stepText { /* Styles */ }

A more flexible approach would be to use the general sibling selector which would match any sibling preceded by the first selector , not only the direct next one:一种更灵活的方法是使用通用兄弟选择器,它可以匹配第一个选择器之前的任何兄弟,而不仅仅是直接的下一个:

.stepTitleAndImages.standard ~ .stepText { /* Styles */ }

The :has pseudo-class is in development by Mozilla , but it hasn't hit any stable browsers yet. :has伪类正在由 Mozilla 开发,但它还没有在任何稳定的浏览器上运行。 With it, and with your HTML structure, you could go:有了它,以及你的 HTML 结构,你可以去:

.stepTitleAndImages:has(.standard) + .stepText { /* Styles */ }

Unfortunately, currently you can't solve this in any other way with CSS (and with your HTML structure) only.不幸的是,目前您无法仅使用 CSS(以及您的 HTML 结构)以任何其他方式解决此问题。

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

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