我查看了W3Schools的网站,该网站解释了如何使用CSS样式化按钮。当按下按钮时,我需要指定一个按钮样式。请问此处应该使用哪个伪类选择器?例如,hover按钮是什么?
我查看了 W3Schools 的网站,该网站解释了如何使用CSS样式化按钮。当点击按钮时,我需要指定一个按钮样式。这种情况下,应该使用哪个伪类选择器呢?例如,hover状态下按钮的样式是:
.button:hover{
}
.button
来为样式化为按钮的锚链接(我在看着你,bootstrap),而且OP在示例中使用了.button
,因此也许可以将其扩展到包括带有:active
的锚点部分?也就是说,如果他们想要一个按钮,他们应该使用<button></button>
,但这不是你的错 :) - Mark Schultheiss如果您只想在鼠标按下时使按钮具有不同的样式,您可以使用:active
伪类。
.button:active {
}
如果您希望在点击后保留样式,那么您将需要使用JavaScript。
按钮有三种状态:
button
button:hover
button:active
正常状态:
.button
{
//your css
}
活动的
.button:active
{
//your css
}
悬停效果
.button:hover
{
//your css
}
片段:
使用 :active
来为按钮的激活状态添加样式。
button:active{
background-color:red;
}
<button>Click Me</button>
很遗憾,不存在`:click`伪选择器。如果您想在点击时更改样式,应使用Jquery / JavaScript。这肯定比纯HTML / CSS的“hack”更好。但是如果您坚持...
input {
display: none;
}
span {
padding: 20px;
font-family: sans-serif;
}
input:checked + span {
background: #444;
color: #fff;
}
<label for="input">
<input id="input" type="radio" />
<span>NO JS styling</span>
</label>
Or, if you prefer, you can toggle the styling:
input {
display: none;
}
span {
padding: 20px;
font-family: sans-serif;
}
input:checked + span {
background: #444;
color: #fff;
}
<label for="input">
<input id="input" type="checkbox" />
<span>NO JS styling</span>
</label>
button:hover 只有在鼠标悬停在按钮上时才会触发。
尝试使用 button:active 替代...同样适用于其他元素
button:active{
color: red;
}
:focus
。 - PeterMader:active
伪类选择器。 - the_mishra:active
是您正在搜索的内容 https://developer.mozilla.org/zh-CN/docs/Web/CSS/:active - Pascal Goldbach