简体   繁体   English

为什么当我使用button:hover时背景颜色不会改变?

[英]Why doesn't background-color change when I use button:hover?

    body{
font-family:"Arial","Lucida Grande", "Lucida Sans Unicode", Verdana, Helvetica, sans-serif;
font-size:12px;
}
p, h1, form, button{border:0; margin:0; padding:0;}
.spacer{clear:both; height:1px;}
/* ----------- My Form ----------- */
.myform{
margin:0 auto;
width:450px;
padding:14px;
}

/* ----------- regForm ----------- */
#regForm{
border:solid 2px #b7ddf2;
background:#A9D0F5;
}
#regForm h1 {
font-size:18px;
font-weight:bold;
margin-bottom:8px;
}
#regForm p{
font-size:11px;
color:#666666;
margin-bottom:20px;
border-bottom:solid 1px #b7ddf2;
padding-bottom:10px;
}
#regForm label{
display:block;
font-weight:bold;
text-align:right;
width:140px;
float:left;
}

#regForm .small{
color:#666666;
display:block;
font-size:11px;
font-weight:normal;
text-align:right;
width:140px;
}
#regForm input{
float:left;
font-size:100%;
padding:4px 2px;
border:solid 1px #2E9AFE;
width:200px;
margin:2px 0 20px 10px;

}
input:focus{
color:#848484;
font-weight:bold;
background-color:#FFC;
}
#regForm select{
float:left;
font-size:12px;
padding:4px 2px;
border:solid 1px #2E9AFE;
width:200px;
margin:2px 0 20px 10px;
}

#regForm button{
clear:both;
margin-left:150px;
width:150px;
height:50px;
background:#2E9AFE no-repeat;
text-align:center;
line-height:32px;
color:#FFFFFF;
font-size:20px;
font-weight:bold;
}
button:hover{
background-color:#FFC;

}

My question is why the color of the background of the button is not changed? 我的问题是为什么按钮背景的颜色没有改变? Thanks. 谢谢。

fyi I have this line in my .html : 仅供参考,我的.html中有这一行:

<button type="submit">Sign-up</button>

It's because the selector #regForm button is more specific than button:hover - see: http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html for a simplified overview on the subject. 这是因为选择器#regForm buttonbutton:hover更为具体 -有关该主题的简化概述,请参见: http : //www.stuffandnonsense.co.uk/archives/css_specificity_wars.html

Using a :hover selector with an id selector solves this: :hover选择器与id选择器一起使用可解决此问题:

#regForm button:hover{
    background-color:#FFC;
}

See: http://jsfiddle.net/32MvE/ 请参阅: http//jsfiddle.net/32MvE/

您必须使用建议的Scartag之类的类或使用属性选择器:

input[type="button"]:hover {background-color:#FFC;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM