The Wayback Machine - https://web.archive.org/web/20240608040938/https://www.geeksforgeeks.org/css-hover-selector/
Open In App

CSS :hover Selector

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The :hover selector CSS pseudo-class is used to style elements when the mouse hovers over them. It can be used on every element.

We can style the links for unvisited pages using the :link selector, for styling the links to visited pages, use the :visited selector & for styling the active link, use the :active selector. If the :link and :visited selectors are present in the CSS definition then in order to see the effect, we must add :hover selector after it.

Syntax:

element :hover{
    // CSS declarations;
}

Example 1: This example illustrates the changing of the background-color on hover over an element.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <style>
    h1:hover {
        color: white;
        background-color: green;
    }
    </style>
</head>
 
<body>
    <h1 align="center"> hover it</h1>
</body>
 
</html>


Output:

Example 2: This example is showing a hidden block on hover over text.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <style>
    ul {
        display: none;
    }
     
    div {
        background: green;
        width: 200px;
        height: 200px;
        padding: 20px;
        padding-left: 50px;
        font-size: 30px;
        color: white;
        display: none;
    }
     
    h3:hover + div {
        display: block;
    }
    </style>
</head>
 
<body>
    <h3 align="center">
        Hover to see hidden GeeksforGeeks.
    </h3>
    <div> GeeksforGeeks </div>
</body>
 
</html>


Output:

Example 3: This example illustrates the changing of the font-color on hover over an element.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <style>
    h1:hover {
        color: red;
    }
    </style>
</head>
 
<body>
    <h1 align="center"> hover it</h1>
</body>
 
</html>


Output:

Example 4: This example illustrates the changing of the font-family of text on hover over it. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <style>
    h1:hover {
        font-family: monospace;
    }
    </style>
</head>
 
<body>
    <h1 align="center"> hover it</h1>
</body>
 
</html>


Output:

Example 5: This example illustrates the changing of the text-decoration to underline on hover over an element.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <style>
    h1:hover {
        text-decoration: underline;
    }
    </style>
</head>
 
<body>
    <h1 align="center"> hover it</h1>
</body>
 
</html>


Output:

Supported Browsers:

  • Google Chrome 1.0 and above
  • Microsoft Edge 12.0 and above
  • Mozilla Firefox 1.0 and above
  • Internet Explorer 4.0 and above
  • Safari 2.0 and above
  • Opera 4.0 and above


Previous Article
Next Article

Similar Reads

CSS :root Selector
The :root selector is used to select all the elements of an HTML Document. This selector covers all the HTML elements or tags. Syntax: :root { // CSS property} Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;root selector&lt;/title&gt; &lt;style&gt; h1 { color: White; } :root { background: green; } body { text-align
1 min read
CSS :not Selector
The :not(selector) selector is used to style every element that is not the specified by selector. Since it prevents specific items from being selected, it is also known as the negation pseudo-class. Syntax: :not(selector) { //CSS Property } Example-1: C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;title
1 min read
CSS :enabled Selector
The :enabled selector is used to set the background color in enabled element in a form. Syntax: :enabled { //property } Example: C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;style&amp;gt; input[type=text]:enabled { background: limegreen; } input[type=text]:disabled { background: silver; } &amp;lt;/style
1 min read
CSS :only-of-type Selector
The :only-of-type selector in CSS represents only those elements that have no siblings of the given type. It is used to set the CSS property to that element. Syntax: :only-of-type { // CSS property } Example 1: In this example, we are using the above-explained selector. C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;:only-o
1 min read
CSS element Selector
The element selector in CSS is used to select HTML elements that are required to be styled. In a selector declaration, there is the name of the HTML element and the CSS properties which are to be applied to that element is written inside the brackets {}. Syntax: element { \\ CSS property } Example 1: This example shows the element selector in CSS.
1 min read
CSS :checked Selector
The checked selector is used to select all checked elements in the input tag and radio buttons. This selector is used with radio buttons, checkbox and option elements. Syntax: :checked { // CSS property} Example 1: In this example, The CSS :checked selector targets checked input elements. It sets a height and width for checked checkboxes and applie
2 min read
What does the “+” (plus sign) CSS selector mean?
The "+" sign selector is used to select the elements that are placed immediately after the specified element but not inside the particular elements. Note: The IE8 and earlier versions &lt;/DOCTYPE&gt; must be declared to work element + element selector. Syntax: element + element { // CSS property } Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;he
1 min read
How to use a not:first-child selector in CSS?
This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element. Syntax: :not( element ) { // CSS property } Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;not first child selector&lt;/title&gt; &lt;style&gt; h1 { color
1 min read
CSS :in-range Selector
The :in-range selector is used to selecting all elements that are given within a particular range value. It is mostly used in an input element with the type min/max attribute. Syntax: :in-range { // CSS property} Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;in range selector&lt;/title&gt; &lt;style&gt; h1 { color
1 min read
CSS :disabled Selector
The: disabled selector is used to select the disabled element. This property is mostly used on the form elements. Syntax: :disabled { // CSS property} You can also Set a background color for all disabled input elements of type="text": input[type=text]:disabled { background: #dddddd;}Example 1: Here is the example of the above-explained selector. C/
2 min read