且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅在元素旁边存在某个元素时才将样式应用于元素

更新时间:2023-01-27 15:45:17

这只适用于< section /> 来自< aside />

<aside>content</aside>
<!-- if there is another node in between, use the '~' selector -->
<section>content</section>

在这种情况下你可以使用一边〜或一边+部分

In that case you could use aside ~ section or aside + section:

section {
    padding: 8px;
}
aside + section {
    width: 500px;
    float: left;
}

在所有其他情况下,你将不得不使用JavaScript,因为这些选择器只能工作从一个方向,从上到下。

In all other cases you'll have to use JavaScript because these selectors only work in one direction, top to bottom.

使用CSS4可能有一种方法使用具有子组合子的选择器的主题,但那是未来。此选择器已从规范中删除。

With CSS4 there might be a way using the Subject of a selector with Child combinator, but that's future. This selector was removed from the specification.