:active - CSS: Cascading Style Sheets 编辑
The :active
CSS pseudo-class represents an element (such as a button) that is being activated by the user. When using a mouse, "activation" typically starts when the user presses down the primary mouse button.
/* Selects any <a> that is being activated */
a:active {
color: red;
}
The :active
pseudo-class is commonly used on <a>
and <button>
elements. Other common targets of this pseudo-class include elements that contain an activated element, and form elements that are being activated through their associated <label>
.
Styles defined by the :active
pseudo-class will be overridden by any subsequent link-related pseudo-class (:link
, :hover
, or :visited
) that has at least equal specificity. To style links appropriately, put the :active
rule after all other link-related rules, as defined by the LVHA-order: :link
— :visited
— :hover
— :active
.
:active
pseudo-class must only apply to the primary button; on right-handed mice, this is typically the leftmost button.Syntax
:active
Examples
Active links
HTML
<p>This paragraph contains a link:
<a href="#">This link will turn red while you click on it.</a>
The paragraph will get a gray background while you click on it or the link.
</p>
CSS
a:link { color: blue; } /* Unvisited links */
a:visited { color: purple; } /* Visited links */
a:hover { background: yellow; } /* Hovered links */
a:active { color: red; } /* Active links */
p:active { background: #eee; } /* Active paragraphs */
Result
Active form elements
HTML
<form>
<label for="my-button">My button: </label>
<button id="my-button" type="button">Try Clicking Me or My Label!</button>
</form>
CSS
form :active {
color: red;
}
form button {
background: white;
}
Result
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of ':active' in that specification. |
Living Standard | |
Selectors Level 4 The definition of ':active' in that specification. |
Working Draft | No change. |
Selectors Level 3 The definition of ':active' in that specification. |
Recommendation | No change. |
CSS Level 2 (Revision 1) The definition of ':active' in that specification. |
Recommendation | No change. |
CSS Level 1 The definition of ':active' in that specification. |
Recommendation | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论