Definition and Usage
The :active pseudo-class selector selects an element from the moment a pointing device (such as a mouse button) is pressed until it is released, while over the element.
- Pointing Device
- A pointing device is a control device that allows users to move a cursor (pointer) on a computer or display screen and select or interact with specific locations. A mouse is a common example of a pointing device. The term "mouse" is also commonly used to refer to pointing devices that move a cursor.
In CSS, the state in which the mouse pointer is over an element and the mouse button is held down is commonly referred to as the "active" state.
Because predefined styles are applied only while the mouse button is held down, this selector can provide an interactive and dynamic user experience.
Note!
A "click" generally refers to the action of pressing and releasing a mouse button.
CSS has no pseudo-class selector for selecting a click event itself; instead, click events are handled using JavaScript's click event.
Syntax
:active {
/* ... */
}
Basic Example
In this example, the button's background color changes from orange to black only while the mouse button is pressed:
<button>Press me!</button>
button {
color: white;
padding: 0.8em 1.5em;
background-color: orange;
border: none;
border-radius: 5px;
}
/* Change the background color only while the button is pressed */
button:active {
background-color: black;
}
Things to Keep in Mind
Styles defined using the :active pseudo-class are overridden by other state-related pseudo-classes (:link, :visited, and :hover) that appear later in the stylesheet and have at least the same specificity. For example, if :active appears before :hover, :hover overrides :active.
Note!
The state-related pseudo-class selectors :link, :visited, :hover, and :active follow a specific ordering rule in a stylesheet. They must be placed in this exact sequence to work as intended.
An easy way to remember this sequence is the LoVe HAte order (LoVe HAte: :link → :visited → :hover → :active). If this order is not followed, a pseudo-class placed later in the stylesheet will override the styles defined by one placed earlier.
Additional Details
The :hover pseudo-class selector selects an element only while the mouse pointer is over it.
<button>Hover over the button, then press it!</button>
button {
color: white;
padding: 0.8em 1.5em;
background-color: orange;
border: none;
border-radius: 5px;
}
/* Note: This declaration has no effect because button:hover overrides the background color. */
button:active {
background-color: black;
}
/* button:hover overrides the background color set by button:active. */
button:hover {
background-color: red;
}
As shown in the example above, :active is placed before :hover. As a result, the color (red) declared in button:hover overrides the color (black) declared in button:active (which selects the button from the moment the mouse button is pressed until it is released, while the mouse pointer is over the element). Therefore, the background color set by button:active is not applied.
The following example shows the correct styling when :hover is placed before :active, following the LoVe HAte order:
button {
color: white;
padding: 0.8em 1.5em;
background-color: orange;
border: none;
border-radius: 5px;
}
button:hover {
background-color: red;
}
button:active {
background-color: black;
}
Note!
On touchscreens, such as those on mobile devices that do not use a mouse, the behavior of :hover and :active when used together for styling may vary between browsers. Their behavior may also be limited or may not match what you expect.
Apply the :active State to a Control Element When Hovering Over Its Label
<label for="user-id">User ID</label>
<input type="text" id="user-id">
input:active {
background-color: red;
}
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
:active
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
:active
|
Selectors Level 4 #the-active-pseudo |