html - 如果存在特定的 CSS 类组合,则更改另一个类的 CSS

标签 html css css-selectors

我还想将“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/

相关文章:

css - 覆盖链接的 theroller 样式

XPath 中的 CSS 选择器

javascript - 使用 javascript 变量设置下拉列表的选定值

java - 通过表单向 servlet 发送大量文本

css - 继承宽度不适用于百分比值

jquery - 伪类 nth-of-type 不适用于特定类

javascript - 如何从 URL 显示图像分辨率

html - 无法摆脱表之间的空间

php - 箭头键更改全屏背景图像

css - susy grid 840如何设置