【发布时间】:2020-09-16 14:10:47
【问题描述】:
快速提问。如果我将鼠标悬停在一个孩子上以定位另一个孩子,或者一个单独的 div 与另一个单独的 div 之间,转换是否有效?
只有将子项放在容器中并将鼠标悬停在父项上,我才能过渡到工作。这是什么规则?
示例 1(不起作用):
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
}
.underline {
width: 0px;
height: 2px;
background-color: black;
transition: 0.4s;
}
h2:hover .underline {
width: 165px;
}
<body>
<h2>Hover Over Me</h2>
<div class="underline"></div>
</body>
示例 2(不起作用)
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 180px;
height: 50px;
}
.underline {
width: 0px;
height: 2px;
background-color: black;
transition: 0.4s;
}
h2:hover .underline {
width: 165px;
}
<body>
<div class="container">
<h2>Hover Over Me</h2>
<div class="underline"></div>
</div>
</body>
示例 3(作品)
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 180px;
height: 50px;
}
.underline {
width: 0px;
height: 2px;
background-color: black;
transition: 0.4s;
}
.container:hover .underline {
width: 165px;
}
<body>
<div class="container">
<h2>Hover Over Me</h2>
<div class="underline"></div>
</div>
</body>
【问题讨论】:
-
CSS selector 中的空格选择前一个选择器的子项。
.thing1 .thing2仅选择类为thing2的元素,前提是它们在thing1中。您的h2:hover .underline与前两个示例中的 HTML 结构不匹配。
标签: html css transition