html - CSS - 将鼠标悬停在 li a 上

标签 html css

我在下面有 HTML 和 CSS。

HTML

<ul>
    <li class="liHover">
        <a href="">Hover List</a>
        <div class="square1">
        Square 1
        </div>
    </li>
    <li class="aHover">
        <a href="">Hover Link</a>
        <div class="square2">
        Square 2
        </div>
    </li>
</ul>   

CSS

li {padding:20px; border: solid 1px;margin:5px;}

.square1, .square2 {
  display: none;
  width: 200px;
  height: 100px;
  border: 1px solid red;
}
.liHover:hover .square1 {
  display: block;
}
.aHover a:hover .square2 {
  display: block;
}

实时样本是 here

如果我将鼠标悬停在 li 上,它会起作用,但如果我将鼠标悬停在 li a:hover 上,它就不起作用。

以下代码有效。

.liHover:hover .square1 {
  display: block;
}

但是下面的代码不起作用。

.aHover a:hover .square2 {
      display: block;
    }

我想知道为什么?以及如何让它发挥作用?

最佳答案

这是因为 .square1.square2 都不是任何 a 标签的子标签。 将 .square1.square2 类添加到 a 标记,或者使 a 成为它们的父级。

关于html - CSS - 将鼠标悬停在 li a 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633303/

相关文章:

javascript - xmlhttprequest 无法加载文件

javascript - Slick 轮播边缘的淡入淡出效果

javascript - 卡在 'contenteditable'

css3 过渡 : different time value when :hover In than Out?

javascript - 替换(淡化)主体背景而不影响嵌套元素

css - 选择时更改单选按钮的边框颜色

python - 如何将包含跨越多列的单元格的 html 表格转换为 Python 3 中的列表列表?

html - css 在 asp.net 中的工作方式不同

javascript - jQuery - 使具有固定标题的表格可排序

javascript - HTML 中 href 链接的悬停效果