gpt4 book ai didi

html - 变换内部 :hover could not activate

转载 作者:太空宇宙 更新时间:2023-11-04 06:52:13 24 4
gpt4 key购买 nike

这是我的代码:

div img{
position: absolute;
}
div img:nth-child(1){
top: 50px;
left: 80px;
transform: scale(0.8);
}
div img:nth-child(2){
top: 50px;
left: 80px;
transform: scale(0.7);
}
img:hover{
transform: scale(1);
}
<html>
<body>
<div>
<img src="pic1.jpg" width="100" height="100"/>
<img src="pic2.jpg" width="100" height="100"/>
</div>
</body>
</html>

然后如果我将鼠标悬停在图片上,它们都无法激活

transform: scale(1);

但是如果我像这样更改 css:

div img:hover{
transform: scale(1);
}

然后 :hover 中的转换按预期正常运行。为什么会这样?非常感谢。

最佳答案

删除所有 css img 样式声明之前的div

它的发生是因为特殊性 - 当您设置更具体的规则时,例如 div img {},它会覆盖不太具体的 css 规则 img {}

About specificity

Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of CSS selectors.

img {
position: absolute;
}

img:nth-child(1) {
top: 50px;
left: 80px;
transform: scale(0.8);
}

img:nth-child(2) {
top: 50px;
left: 80px;
transform: scale(0.7);
}

img:hover {
transform: scale(1);
}
<div>
<img src="pic1.jpg" width="100" height="100" />
<img src="pic2.jpg" width="100" height="100" />
</div>

关于html - 变换内部 :hover could not activate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52753917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com