下面的代码在按钮被选中时将按钮文本变为深色。我假设这来自Bootstrap的btn类中嵌入的代码。选择按钮后,如何重写代码以阻止文本更改颜色?
<html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<style>
.buttonColor{
color:#ff0000;
}
.buttonColor:hover{
color:#ffff00;
}
</style>
</head>
<body>
<button class="buttonColor btn" > Submit</button>
</body>
</html>发布于 2016-04-27 23:13:20
这是一个常见的问题:How to overwrite styling in Twitter Bootstrap,best way to override bootstrap css,不胜枚举。
在CSS law of specifity上阅读。本质上,如果您在类声明中更具体,您可以覆盖其他以相同元素为目标的元素:
在您的示例中:
button.buttonColor.btn {
color: red;
padding: 50px;
}将覆盖BootStrap的button.btn声明。
类似地,添加伪选择器以覆盖其他状态:
button.buttonColor.btn:active、button.buttonColor.btn:hover等
发布于 2016-04-27 23:11:30
假设您所说的"selected“指的是按钮的active状态,您可以这样实现它:
.buttonColor:active {
color: #ffff00;
}发布于 2016-04-27 23:13:42
Bootstrap使用:hover、:active和:focus伪类来定位特定的元素状态:
/* Example of Bootstrap :active styles for buttons */
.btn.active, .btn:active {
background-image: none;
outline: 0;
-webkit-box-shadow: inset 0 3px 5px rgba(0,0,0,.125);
box-shadow: inset 0 3px 5px rgba(0,0,0,.125);
}同时还包括:
/* Example of Bootstrap :focus and :hover styles for buttons */
.btn.focus, .btn:focus, .btn:hover {
color: #333;
text-decoration: none;
}因此,您只需使用自己的样式显式覆盖它们:
/* The more specific your selector (e.g. the more accurately it describes an */
/* element, the more likely a style will be applied. */
.btn.buttonColor:active,
.btn.buttonColor.active,
.btn.buttonColor:hover,
.btn.buttonColor:focus {
color: #ffff00!important;
}或者,如果您想说得更具体一些,可以显式地专门针对<button>元素:
button.btn.buttonColor:active,
button.btn.buttonColor.active,
button.btn.buttonColor:hover,
button.btn.buttonColor:focus {
color: #ffff00!important;
}https://stackoverflow.com/questions/36893758
复制相似问题