Languages
[Edit]
EN

CSS - display child element on hover

3 points
Created by:
sienko
743

In this article, we would like to show you how to display child element on hover using CSS.

Quick solution:

.parent .child {
    display: none;
}

.parent:hover .child {
    display: block;
}

The :hover CSS pseudo-class is triggered when the user hovers over an element with the cursor, changing the element style to the one specified within curly brackets.

 

Practical example

In this example, we display child element on hover with the following steps:

  1. specify the style of a parent element,
  2. use class selector .parent .child to access the child element and specify display: none; to make it invisible at the start,
  3. add on-hover event to the parent element using .parent:hover and change the visibility of a child using display: block; so it will show up when you hover the parent.
// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    .parent {
        padding: 10px;
        background: deepskyblue;
        width: 100px;
        height: 100px;
        color: white;
    }

    .parent .child {
        display: none;   /* hides element by default */
    }

    .parent:hover .child {
        display: block;  /* shows element on hover   */
    }

  </style>
</head>
<body>
  <div class="parent">
    <div class="child">Child content...</div>
  </div>
</body>
</html>

 

References

Alternative titles

  1. CSS - display child element's content on hover
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join