我还想将“three”的背景颜色更改为“lightblue”,但前提是页面上存在类别组合“one current”。注意:“三”不能获得额外的等级。我知道,您可以使用 JavaScript 做到这一点,但是否还有原生 CSS 解决方案?
这是我的例子:
<!DOCTYPE html>
<html>
<head>
<style>
.one.current {background-color: lightblue;}
</style>
</head>
<body>
<header>
<p class="one current">The first paragraph.</p>
</header>
<main>
<p class="two">The second paragraph.</p>
<p class="three">The third paragraph.</p>
<p class="four">The fourth paragraph.</p>
</main>
</body>
</html>
最佳答案
在您的情况下,您可以使用 .one.current
在<header>
然后样式.three
和:
.one.current ~ main .three
还有 <p>
header 内的元素:
.one.current p
Documentation about the
~
operator: general sibling selector (MDN).
关于html - 如果存在特定的 CSS 类组合,则更改另一个类的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51162215/