Dofactory.com
Dofactory.com
Earn income with your CSS skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

CSS Hover

Hovering means moving the mouse/cursor over an element.

Hovered elements have a different state which can be styled differently.

The :hover pseudo-class is triggered when hovering over an element.

Example

#

Hover over the link and see the style change.

Mouse over this link
<style>
  .hover-a {
    color: steelblue;
  }

  .hover-a:hover {
    color: navy;
    font-weight: bold;
    font-style: italic;
  }
</style>

<a class="hover-a"
   href="javascript:void(0)">Mouse over this link</a>

The :hover selector

The :hover selector selects elements that are hovered.

The default style of an element can be overridden using this selector.

This selector works on any elements that can be hovered.

Mouse over the heading or paragraph below and see the style change.

Hover this heading

Hover this paragraph

<style>
  .hover-element {
    padding: 15px;
  }

  .hover-element:hover {
    background-color: moccasin;
  }
</style>

<h3 class="hover-element">Hover this heading</h3>
<p class="hover-element">Hover this paragraph</p>

More Examples

Hover the text below to display a 'tooltip'.

Hover to display tooltip
Tooltip displayed!
<style>
  .hover-tooltip {
    background-color: #dfe7ff;
    padding: 10px;
    margin: 10px;
    display: none;
    width: 200px;
  }

  .hover-div:hover + .hover-tooltip {
    display: block;
  }
</style>

<div class="hover-div">Hover to display tooltip</div>
<div class="hover-tooltip">Tooltip displayed!</div>

Hover transition

By default, a style change due to :hover occurs instantly, .

For a nice effect, a CSS transition can animate a style change.

Hover over the <button> element and see how the hover style is applied smoothly.

<style>
  .hover-button {
    padding: 10px;
    width: 150px;
    border: 1px solid #ddd;
    transition: .7s ease;
  }

  .hover-button:hover {
    background-color: #302ea3;
    color: white;
  }
</style>

<button class="hover-button">Hover me</button>

You may also like


Last updated on Sep 30, 2023

Earn income with your CSS skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides