快速提问。如果我将鼠标悬停在一个孩子上以定位另一个孩子,或者将一个单独的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>
答案 0 :(得分:1)
* {
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>
您可以检查This中的CSS选择器