Definition and Usage
The :hover pseudo-class selector selects an element while a pointing device (such as a mouse cursor) is over it.
- 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 is commonly referred to as the "hover" state.
Because predefined styles are applied only while the user hovers the mouse pointer over an element, this selector can provide an interactive and dynamic user experience.
Syntax
:hover {
/* ... */
}
Basic Example
In this example, the button's background color changes from orange to red only while the mouse pointer is over the button:
<button>Hover over me!</button>
button {
color: white;
padding: 0.8em 1.5em;
border: none;
border-radius: 5px;
background-color: orange;
}
/* Change the background color only while hovering over the button */
button:hover {
background-color: red;
}
Things to Keep in Mind
Styles defined using the :hover pseudo-class are overridden by other state-related pseudo-classes (:link and :visited) that appear later in the stylesheet and have at least the same specificity. Also, 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 :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.
<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 :hover State to a Control Element When Hovering Over Its Label
<label for="user-id">User ID</label>
<input type="text" id="user-id">
input:hover {
background-color: red;
}
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
:hover
|
1 | 12 | 1 | 2 |
Specifications
| Specification | |
|---|---|
:hover
|
Selectors Level 4 #the-hover-pseudo |