如何通过仅满足此条件将样式应用于 .apply-style-here 的父 div
<div class="first-stage">
<div>
<div class="apply-style-here">Content</div>
</div>
</div>
<div class="first-stage">
<div class="second-stage">
<div>
<div class="apply-style-here">Content</div>
</div>
</div>
<div>
<div class="apply-style-here">Content</div>
</div>
</div>
尝试解决办法
/* Result: Put border to all parent of apply-style-here class */
.first-stage:has(div > .apply-style-here) > div {
/* margin-top: 20px; */
border: 1px solid red;
}
/* Result: No styles applied at all */
.first-stage:not('div > .second-stage') > div {
border: 1px solid blue;
}
/* Result: No styles applied at all */
.first-stage:has(div:not('.second-stage') > .apply-style-here) > div {
border: 1px solid blue;
}
/* Result: Applied to all .apply-style-here even second .second-stage is present */
.first-stage:has(div:not(.second-stage) > .apply-style-here) > div {
border: 1px solid blue;
}
/* Result: Applied to all .apply-style-here even second .second-stage is present */
.first-stage:has(div:not(.second-stage > .apply-style-here)) > div {
border: 1px solid blue;
} Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
根据您的描述,以下选择器可以工作: