The Wayback Machine - https://web.archive.org/web/20240916010722/https://www.geeksforgeeks.org/how-to-display-element-on-hover-using-css/
Open In App

How to Display Element on Hover using CSS ?

Last Updated : 22 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In web development, displaying an element on hover is a common interaction pattern used to reveal additional content or actions.

Below are the various CSS approaches to achieve this effect:

Using the display property

This approach makes a hidden element appear when we hover over another element. It works by changing the display property of the hidden element from none to block when we hover over the element that contains it.

Example: The below code will explain the use of the display property to display elements on hover in CSS.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        Display Element on Hover
    </title>
    <style>
        h3,
        .parent-element {
            text-align: center;
        }

        .hidden-element {
            display: none;
        }

        .parent-element:hover 
        .hidden-element {
            color: blue;
            display: block;
            margin: auto;
            height: 50px;
            width: 200px;
            border: 1px solid green;
            border-radius: 5px;
        }
    </style>
</head>

<body style="text-align: center;">
    <h2>
        Using CSS display property
    </h2>
    <h3>
        Hover the below text to see
        <br/>hidden content
    </h3>
    <div class="parent-element">
        Hover over me <br/>
        <div class="hidden-element">
            I'm visible now!
        </div>
    </div>
</body>

</html>

Output:

fosiGIF

Using the visibility property

This approach involves toggling the visibility property of an element between hidden and visible to show or hide it on hover. Using visibility allows the element to remain in the layout, affecting the positioning of other elements around it.

Example: The below code uses the visibility property to display elements on hover using CSS.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        Display Element on Hover
    </title>
    <style>
        h3,
        .parent-element {
            text-align: center;
        }

        .hidden-element {
            visibility: hidden;
            opacity: 0;
            transition: 
                visibility 0s, opacity 0.5s ease;
        }

        .parent-element:hover 
        .hidden-element {
            color: green;
            visibility: visible;
            opacity: 1;
        }
    </style>
</head>

<body style="text-align: center;">
    <h2>
        Using CSS visibility property
    </h2>
    <h3>
        Hover the below text to see the 
        <br/> hidden content.
    </h3>
    <div class="parent-element">
        Welcome to GFG!
        <div class="hidden-element">
            GeeksforGeeks is a olnline 
            learning platform
        </div>
    </div>
</body>

</html>

Output:
fosiGIF



Similar Reads

How to display text on hover over image using Tailwind CSS in React.js ?
In this article, we'll see how the text appears on hover over an image using tailwind CSS in a ReactJS app. With the help of Tailwind CSS utility classes, we can display the text or image or anything after hovering over an element in Reactjs. Prerequisites:NPM &amp; Node.jsReact JSTailwindCSSApproach:To display text hover over the image using Tailw
4 min read
How to display button on hover over div in Tailwind CSS ?
Tailwind CSS provides a huge number of CSS utility classes that allow you to quickly apply to style and easily construct custom designs. In this article, we’ll see how to display the button when hovering over the div in TailwindCSS. Example 1: Create a &lt;div&gt; element and apply group and relative CSS classes to make it a parent element and to p
2 min read
How to Style Parent Element when Hover Over a Child Element in CSS ?
In this article, we will learn how to style the parent element when hovering over a child element. Styling the parent element when hovering over a child element is a useful CSS technique that allows you to change the appearance of the parent element when the mouse cursor is hovering over one of its child elements. This can be useful for adding inte
2 min read
What is the difference between display: inline and display: inline-block in CSS?
The display property in CSS is a very useful and commonly used property that contains many values. The display property defines how an HTML element should be displayed on the webpage. The property also specifies the type of box used for an HTML element and how it should be laid out on the page. If we need to display the elements that are laid out a
4 min read
How display: inline is different from display: inline-block in CSS ?
In this article, we will know about the display property in CSS, along with understanding the 2 different property values for display property, i.e., display: inline &amp; display: inline-block properties, &amp; will understand their basic differences &amp; implementation through the examples. The display property facilitates setting the element by
3 min read
What is the difference between display:inline-flex and display:flex in CSS ?
In this article, we will see the display property &amp; its values, i.e. the inline-flex &amp; flex, in order to specify the display behavior of an element, along with understanding their basic differences &amp; will illustrate them through the examples. Both CSS display: inline-flex and display: flex properties are used to create flexible layouts.
3 min read
How to zoom an element on Hover using CSS ?
In this article, we learn about how to zoom on hover using CSS. It is called pseudo-selector and used to select all the elements when the user moves the mouse over the elements. When the user hovering on the element the scale function is called to increase the dimensions of the element. It can be used on all the elements. An element must be declare
1 min read
How to change opacity of every data element on hover in a table using CSS ?
There is a Table made by using HTML in which data elements are set to a low opacity. The task is to change the opacity of a particular element when user hover over data elements. To make a table look better, we will use gradient colors to set backgrounds of the table data elements. In this approach, we will be using the opacity property of CSS to c
2 min read
How to add border to an element on mouse hover using CSS ?
We have given a web page containing elements and the task is to add border to an element on mouse move over (hover) using CSS. When we add a border to an element on hovering the mouse, it affects the position of the other nearest element. To remove this problem, we can use the CSS margin property. Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html
1 min read
Display div element on hovering over &lt;a&gt; tag using CSS
We will render the div element by hovering over the &lt;a&gt; tag using CSS. We can apply an adjacent sibling CSS selector property to the &lt;a&gt; tag that will display the hidden div element's content while hovering over it. The Adjacent sibling selector is a CSS Combinators, used to select the element that is adjacent or the element that is nex
2 min read
Underline Hover Animation Effect using Tailwind CSS
The Tailwind CSS underline animation is a visual effect that can be added to any text or heading or a specific word in the web page using the Tailwind CSS utility framework. The basic function of this effect is this creates an underline animation from left to right with a smooth transition, in simpler words it created underline animation whenever w
3 min read
How to Shift Inline Elements When Text Bold on Hover using CSS ?
When we will use a:hover pseudo-class to add a bold effect to the inline elements then we observe that whenever we hover over elements with the mouse, the elements to the right side of the mouse gets shifted to the right. This is not a good user experience thus needs to be removed. We can use the letter-spacing CSS property to fix this issue and ma
2 min read
How to create paper corner fold effect on hover by using HTML and CSS?
The folding effect is quite attractive on the website, may you have seen on some websites when you hover on the paging layout it gets folded on the corner. The folding corner effect can be achieved by using HTML and CSS only. The below sections will guide you on how to create the animation. In this article, we will divide the article into two secti
3 min read
How to Create Animated Navigation Bar with Hover Effect using HTML and CSS ?
The Navigation bar or navbar or menu-bar is the most important component of any web or mobile application. The user can only navigate from one page to another page through this menu. It is usually provided at the top of the website to provide a better UX (user experience). Approach: The approach is to create a navbar first and then animate a line b
3 min read
Shake a Button on Hover using HTML and CSS
The shake button effect also known as the wiggle effect can be used to make the website look more responsive and dynamic. This is one of the best effects to understand the concept of @keyframes rule in CSS. Approach: The shake button effect or animation can be created using HTML and CSS, First, we will create a basic button using HTML and then we w
2 min read
How to flip an image on hover using CSS ?
In this article, you will learn how to flip an image (add mirror effect), both horizontally and vertically when the mouse has hovered over it. This can be done by applying the transformation to the image as shown in the following example: Example 1: This example represents how to flip image horizontally by transforming it along the X-axis using tra
1 min read
How to rotate a text 360 degrees on hover using HTML and CSS?
The Text can be rotated 360 degrees using basic HTML and CSS, this animation can be used as a heading or subheadings in the website to make it look more attractive. The below sections will guide you on how to create the desired effect. HTML Code: In this section we will create a basic div element which will have some text inside of it. C/C++ Code
2 min read
How to show/hide dropdown menu on mouse hover using CSS ?
The approach of this article is to show and hide the dropdown menu on mouse hover using CSS. The task can be done by using the display property and :hover selector. Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;title&gt; How to Show Hide Dropdown Using CSS &lt;/title&gt; &lt;style&gt; ul { padding: 0; l
2 min read
How to apply Amazing Hover Effect over Button using HTML and CSS ?
You have visited many websites and you have seen many hover buttons present in those websites. So, such Hover Button animation effect can be easily generated by using HTML and CSS. By using HTML we will design the basic structure of the button and then by using the properties of CSS, we can create the hover animation effect. A sample video is provi
3 min read
Split Skewed Button Hover Animation using CSS
Split Skewed Button Hover Animation effect can be created using the before and after pseudo-classes in CSS. On hovering change skewness and swap the positions of before and after pseudo-class. Steps: Create an HTML file named index.html.Create a div element for the button.Add styles to the button.On both the before and after pseudo-class of the but
2 min read
Tiling Perspective List Grid Hover Animation using CSS
Tiling Perspective List Grid Hover Animation can be created by using the nth-child CSS property and a display grid. Each column element styles and hover effects are different and this effect gives a nice perspective look . Approach: Create an HTML file named index.html.Create a ul list element in HTML.Create some li list elements inside the ul crea
3 min read
How to create icon hover effect using CSS ?
CSS: hover Selector: The: hover selector (or CSS pseudo-class) matches when the user interacts with an element with a pointing device. It is generally triggered when the user hovers over an element with the cursor (mouse pointer). Approach: 1: All the anchor tags are targeted with the CSS declarations (within the braces) being hovered with the mous
2 min read
How to make Google search bar like input box highlight on hover using CSS ?
In this article, we will learn how to make the input box highlight on hover like the Google search bar. Approach: We will use the CSS hover property so that whenever we hover over an element we can have some animations. For the input box, there is a box-shadow property that we can use to specify our dimensions of shadow. Syntax: element :hover{ //
2 min read
How to Modify Hover Effect using Tailwind CSS ?
In Tailwind CSS, the term "modify hover" refers to changing the styles that are applied to an element when it is hovered over. With Tailwind CSS "hover" variation, you can quickly apply particular utility classes and custom classes to control how components appear when you hover over them, giving you flexibility in creating hover effects for your w
3 min read
Design a Letter Hover Effect using HTML CSS and JavaScript
In this article, we will learn to implement the bouncing letter hover effect using simple HTML, CSS, and JavaScript. HTML is used to create the structure of the page, CSS is used to set the styling and JavaScript is used for animation. Approach to Create the Bouncing Letter Hover EffectHTML Code: To achieve a dynamic bouncing text effect, start by
2 min read
How to create a Smooth Transition on hover using CSS ?
Creating a smooth transition on hover in CSS involves using the transition property to define the property being animated and the duration of the transition. This effect is commonly used to make changes to an element's appearance smooth and visually appealing when a user hovers over it. Syntax:/* Example of creating a smooth transition on hover */.
1 min read
How to Change Image Opacity on Hover using CSS ?
Changing the opacity of an image on hover can add a more elegant and interactive effect to your website. This simple yet effective technique can be implemented using CSS. When a user hovers over an image, it gradually becomes more transparent, pointing out the interaction and creating an attractive look. These are the following approaches: Table of
2 min read
How to Create an Image Overlay Fade in Text Effect on Hover using CSS ?
In the context of web design, it's all about making dynamic and interesting user experiences(UI). Adding picture overlay effects to the website is a good method to improve the aesthetic appeal, especially when users interact with it by hovering over images. This article will walk you through the steps to create a seamless image overlay fade-in text
3 min read
How to Change the Color of Link on Hover using CSS ?
Changing the color of a link on hover can provide visual feedback to users, indicating that the link is interactive. It improves the user experience by helping users understand that the link is clickable. These are the following approaches: Table of Content Using CSS pseudo-classUsing CSS VariablesUsing Inline CSSUsing CSS pseudo-classCSS provides
2 min read
How to Fade a Button on Hover using CSS ?
Fading a button on Hover in CSS is a visually engaging effect that can be done using various approaches. Below are the approaches to fade a button on hover using CSS: Table of Content Using opacity PropertyUsing Linear Gradient with RGBA ValueUsing opacity PropertyThis approach utilizes the CSS opacity property to achieve a button fade effect upon
2 min read