Html CSS:当我为段落设置ID时,悬停效果不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21705058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS :hover effect not working when I set an ID to the paragraph
提问by 023023
I have the following css3 transition with ease effect:
我有以下 css3 过渡效果:
HTML
HTML
<div class="button">
<a href="#" onMouseOver="clicksound.playclip()"></a>
<p id="myId" class="top"></p>
</div>
CSS
CSS
* {
padding: 0;
margin: 0;
}
.button {
width: 180px;
margin-top: 45px;
}
.button a {
display: block;
height: 40px;
width: 180px;
/*TYPE*/
color: black;
font: 17px/50px Helvetica, Verdana, sans-serif;
text-decoration: none;
text-align: center;
text-transform: uppercase;
}
.button a {
background:url(http://imageshack.com/a/img819/761/dqj.gif);
margin: -50 0 0 0;
z-index: -1;
}
p#myId {
background: url(http://imageshack.com/a/img854/1921/9ft3.png);
display: block;
height: 40px;
width: 167px;
margin: -40px 0 0 5px;
z-index:-1;
/*TYPE*/
text-align: center;
font: 12px/45px Helvetica, Verdana, sans-serif;
color: #fff;
/*POSITION*/
position: absolute;
/*TRANSITION*/
-webkit-transition: margin 0.1s ease;
-moz-transition: margin 0.1s ease;
-o-transition: margin 0.1s ease;
-ms-transition: margin 0.1s ease;
transition: margin 0.1s ease;
}
.button:hover .top {
margin: -67px 0 0 5px;
line-height: 35px;
}
/*ACTIVE*/
.button:active .top {
margin: -70px 0 0 5px;
}
If I change the p#myId
selector to p
in CSS, it works (the button goes up on hover), otherwise it won't.
如果我将p#myId
选择器更改为p
CSS 中的,它会起作用(按钮在悬停时会上升),否则不会。
回答by Andrea Ligios
The problem is that the selector handling your :hover
behavior has a lower Specificitythan the rule for the default behavior (p#id
selector).
问题是处理您的:hover
行为的选择器的特异性比默认行为(选择器)的规则低p#id
。
Changing this
改变这个
.button:hover .top {
to this
对此
.button:hover #myId.top {
will solve the problem: Running example
将解决问题: Running example
You can also apply an id to a parent object (lets' say <div id="container">
), and then use
您还可以将 id 应用于父对象(比方说<div id="container">
),然后使用
#container .button:hover .top {
A must-read: Specifics on CSS Specificity
必读:CSS 特异性细节
Examples:
例子: