html - 带悬停的 CSS 条件语句

标签 html css sass

这个问题在条件 CSS 中可能更简单。

我有五行内容,根据某些条件,我添加了一个名为 'previous' 的类以显示不同的背景颜色。

当我将鼠标悬停在上一节课的内容上时,背景变得透明,这不太好。

即使鼠标悬停,我也想显示相同的背景颜色(灰色)。

所以,我尝试了下面的代码并尝试在 css 中使用 :has 条件,如果它有以前的类,将悬停颜色更改为灰色。但它没有用。

我的期望是即使悬停也有相同的背景颜色。

谁能帮我解决这个问题,因为我只需要 CSS/SCSS 的解决方案。不是来自 javascript。

.previous {
  background-color: grey;
}
.row:hover {
  background-color: transparent;
  :has(.previous) {
      background-color: grey;
  }
}
<div class="row">Some Content 1</div>
<div class="row previous">Some Content 2</div>
<div class="row">Some Content 3</div>
<div class="row previous">Some Content 4</div>
<div class="row">Some Content 5</div>

最佳答案

您可以使用 :not 伪类来执行此操作,以从悬停效果中排除具有 previous 类的行。现在 :hover 将在每个具有 .row 类但不具有 .previous 类的元素上运行。

.row {
  background-color: lightblue;
}

.previous {
  background-color: grey;
}

.row:not(.previous):hover {
  background: transparent;
}
<div class="row">Some Content 1</div>
<div class="row previous">Some Content 2</div>
<div class="row">Some Content 3</div>
<div class="row previous">Some Content 4</div>
<div class="row">Some Content 5</div>

关于html - 带悬停的 CSS 条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57107851/

相关文章:

html - 如何只选择同名的第一个 div?

python - 在 Python 中运行基本的 Selenium 脚本时为 "Firefox quit unexpectedly."

css - 一个div的高度等于另一个

html - 以名称值对形式列出所有 html 实体

ruby - Sencha Cmd v4.0.2.67 创建后台进程失败

javascript - 了解 window.close() 是否会显示安全警告

javascript - 在 phpbb 中按提交后网站无法连接

css - 变换在同一旋转中旋转度数

html - 文件 .scss 未在构建中生成

ruby-on-rails - 如何修复Error : “Sass::SyntaxError in Posts#index” in Ruby on Rails?