html - 悬停后保持元素被选中

标签 html css hover

这是我的代码:

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown:hover .dropdown-content {
  display: block;
}

#image1:hover {
  filter: brightness(50%);
}

.desc {
  padding: 15px;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>

  <h2>Dropdown Image</h2>
  <p>Move the mouse over the image below to open the dropdown content.</p>

  <div class="dropdown">
    <img id="image1" src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="100" height="50">
    <div class="dropdown-content">
      <img src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="300" height="200">
      <div class="desc">Beautiful Cinque Terre</div>
    </div>
  </div>

</body>

</html>

如您所见,当您运行它并将鼠标悬停在小照片上时,会出现更大的版本(而较小的版本会变得更暗)。尽管如此,当您将鼠标从较小的版本悬停到较大的版本时,小照片会恢复正常。

有人可以向我解释当光标位于小照片或大照片上时如何使用 CSS 使较小的照片变暗(选中)吗?

非常感谢您。

最佳答案

.dropdown {
    position: relative;
    display: inline-block;
}
.dropdown:hover #image1 {
    filter: brightness(50%);
}
.dropdown:hover .dropdown-content {
    display: block;
}
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}
.desc {
    padding: 15px;
    text-align: center;
}
<div class="dropdown">
  <img id="image1" src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="100" height="50">
  <div class="dropdown-content">
    <img src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="300" height="200">
    <div class="desc">Beautiful Cinque Terre</div>
  </div>
</div>

关于html - 悬停后保持元素被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51687353/

相关文章:

html - 如何使两个内联元素适应主体宽度?

javascript - 如何保持 DRY - 多个输入在按键时触发相同的功能?

android - Android TextView 中的 CSS

html - 悬停标题文本和背景的 native /默认颜色

html - CSS - 我在 codepen 上遇到了一个 css 动画编码问题

html - LESS 编译的 css 中动态属性的最佳方法

HTML/CSS 一页网站仅滚动某些 div

css:在扩展父级时匹配元素的宽度

CSS bootstrap 悬停工具提示不起作用

CSS 悬停在 li child 上