CSS :hover Pseudo-class
Example
Select and style a link when you mouse over it:
a:hover
{
background-color: yellow;
font-size: 18px;
}
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The CSS :hover
pseudo-class is used to select elements when you mouse over them.
Tip: The :hover
pseudo-class can be used on all elements, not only
on links.
Tip: Use :link to style links to unvisited pages, :visited to style links to visited pages, and :active to style the active link.
Note: :hover MUST come after :link and :visited (if they are present) in the CSS definition, in order to be effective!
Version: | CSS1 |
---|
Browser Support
The numbers in the table specifies the first browser version that fully supports the pseudo-class.
Pseudo-class | |||||
---|---|---|---|---|---|
:hover | 4.0 | 7.0 | 2.0 | 3.1 | 9.6 |
CSS Syntax
More Examples
Example
Select and style a <p>, <h1> and <a> element when you mouse over it:
p:hover, h1:hover, a:hover {
background-color: yellow;
}
Try it Yourself »
Example
Select and style unvisited, visited, hover, and active links:
/* unvisited link */
a:link {
color: green;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: red;
}
/* selected link */
a:active {
color: yellow;
}
Try it Yourself »
Example
Style links with different styles:
a.ex1:hover, a.ex1:active {
color: red;
}
a.ex2:hover, a.ex2:active {
font-size: 150%;
}
Try it Yourself »
Example
Hover over a <span> element to show a <div> element (like a tooltip):
div {
display: none;
}
span:hover + div {
display: block;
}
Try it Yourself »
Example
Show and hide a "dropdown" menu on mouse hover:
ul {
display: inline;
margin: 0;
padding: 0;
}
ul li {display: inline-block;}
ul li:hover {background: #555;}
ul li:hover ul {display: block;}
ul li ul {
position: absolute;
width: 200px;
display: none;
}
ul li ul li {
background: #555;
display: block;
}
ul li ul li a {display:block !important;}
ul li ul li:hover {background: #666;}
Try it Yourself »
Related Pages
CSS tutorial: CSS Links
CSS tutorial: CSS Pseudo classes