:hover - Selector CSS

:hover

Summary of characteristics of the :hover selector

Quick description
A pseudo-class targeting the element on which the mouse cursor is located.
Status
Standard
W3C Module
Selectors
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: REC (Recommendation)

Document status: REC (Recommendation)

Description of the :hover pseudo-class.

The :hover pseudo-class targets elements that are hovered by the mouse pointer, and during the time when this pointer is above them. The mouse button does not need to be pressed for :hover to be activated.

Touchscreens are special.

Careful! With a touchscreen (phones, tablets, or desktop touch screens) the :hover selector is never activated. Support for :hover can be tested with media queries (refer to the @media directive for further explanation):

@media (hover: hover) { a:hover { background:yellow; } } @media (hover: none) { a:active { background:yellow; } }

Examples of how to use :hover.

Choosing properties to use with :hover.

Pretty much any property can be applied to the :hover selector. However, we prefer those that do not require a recalculation of the layout. The border property, for example, adds a border that shifts the entire rest of the page, while the outline adds an outline that overlaps with elements already in place, so you don't need to move them. This distinction is clearly visible in the example below.

Border (property border) - Outline (property outline) - background color

Improved visual effect with a transition.

The transition effect consists of gradually varying the value of a property. For more on this, see the transition property. The combination of a transition and the :hover selector often results in interesting visual effects.

Border with transition effect - Other transition effect

Using :hover on a pseudo-element.

The :hover pseudo-class can be combined with a selector of the pseudo-element type. The effect will be as follows: when the pointer hovers over the element, the styles are applied to the pseudo-element. You can test this on the examples below.

:hover acts here on the pseudo-element ::before.

Here, it is the selected portion of text that is made reactive with :hover.

Browser compatibility with :hover.

The pseudo-class :hover is very well recognized by browsers. Initially its use was reserved for elements of type a, but it is now applicable to all types of elements, and also to pseudo-elements.

Column 1
Support for the :hover pseudo-class targeting the element the mouse is on.
Column 2
Support for the use of the :hover pseudo-class on type a elements.
Column 3
Support for the use of the :hover pseudo-class on all elements.
Column 4
Support for the :hover pseudo-class on pseudo-elements (such as ::before or ::after for example).

Notes:

(1) On Safari, clicking a clickable element changes the element to the :hover state. The element will remain in the :hover state until another element has entered the :hover state.

(2) On Internet Explorer, clicking on an element and scrolling through the content (without moving the cursor) does not disable :hover.

1
:hover
pseudo-class
2
:hover
on links a
3
:hover
on all elements
4
:hover
on pseudo-elements
Estimated overall support.
97%
97%
97%
95%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

Opéra Mobile

KaiOS Browser

Samsung Internet

Opéra

Safari

Safari sur IOS

Chrome

Firefox

Edge

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Baidu Browser

QQ Browser

UC Browser pour Androïd

Opéra mini

Historic of the :hover pseudo-class.

  • Cascading Style Sheets Level 2 - Revision 1 Specification

    Introduction of the :hover pseudo-class in CSS version 2.xx.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • Selectors Level 3

    No change to the :hover pseudo-class.
    WD
    August 03, 1999
    Working Draft.
    CR
    November 13, 2001
    Candidate Recommendation.
    PR
    December 15, 2009
    Proposed for recommendation.
    REC
    September 29, 2011
    Recommendation .
  • Selectors Level 4

    No change to the :hover pseudo-class.
    WD
    September 29, 2011
    Working Draft.
    CR
    PR
    REC

See also...

The following pseudo-classes allow you to designate an element according to the user's actions: mouse movements, selecting an input box, link, etc.

:active
Langue française
A pseudo class targeting the element that is active (the one that is clicked or tapped).
:focus
Langue française
A pseudo-class targeting the input area that has focus.
:focus-visible
Langue française
A pseudo-class targeting the input area that has focus, but only if the browser materializes this focus (by a thicker border for example).
:focus-within
Langue française
A pseudo-class targeting the input box that has focus, as well as the corresponding form.