The Wayback Machine - https://web.archive.org/web/20240618200117/https://www.geeksforgeeks.org/how-to-style-parent-element-when-hover-over-a-child-element-in-css/
Open In App

How to Style Parent Element when Hover Over a Child Element in CSS ?

Last Updated : 07 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 interactivity and visual feedback to your website or web application.

Syntax:

child-element {
    pointer-events: auto
}
child-element:hover {
    /* Styles for child element */
}
parent-element {
    pointer-events: none
}
parent-element:hover {
    /* Styles for parent element */
}

Approach: In this approach, we are using the hover selector to apply styles to both the child and parent elements of the HTML structure. The CSS code applies the background color of yellow to the button element when it is being hovered over by using the child selector, and then applies the background color to the div element when the button is being hovered over by using the adjacent sibling selector.

 

Example 1: In this example, we have a simple HTML structure with a parent element div and a child element button. Here is an example CSS code that will change the background color of the parent div when the button is being hovered over.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        /* Apply styles to parent element */
        .parent {
            pointer-events: none;
        }
  
        .parent:hover {
            background-color: rgb(132, 233, 189);
        }
  
        /* Apply styles to child element */
        .parent .child {
            pointer-events: auto;
        }
  
        .parent .child:hover {
            background-color: yellow;
        }
    </style>
</head>
  
<body>
    <div class="parent">
        <h1 style="color:green">GeeksforGeeks</h1>
        <button class="child">Hover over me</button>
    </div>
</body>


Output:

How to style the parent element when hovering over a child element?

Styling the parent element when hovering over a child element

Example 2: In this example, when hovering over any of the list items within the <ul> element, the text color of the list item will change to red and the color of the parent element change to light blue.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        /* Apply styles to parent element */
        .parent {
            pointer-events: none;
        }
  
        .parent:hover {
            background-color: lightblue;
        }
  
        /* Apply styles to child element */
        .parent ul li {
            pointer-events: auto;
        }
  
        .parent ul li:hover {
            color: red;
        }
    </style>
</head>
  
<body>
    <h1 style="color:green">
          GeeksforGeeks
      </h1>
    <div class="parent">
        <h2>Parent Element</h2>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
        </ul>
    </div>
</body>
  
</html>


Output:

How to style the parent element when hovering a child element?

Styling the parent element when hovering over a child element



Similar Reads

How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to apply style to parent if it has child with CSS?
We know how to apply styles to the child elements if a parent class has one. But if we want to apply a style to the parent class that with CSS. Here’s the way we can do that. A child combinator describes a parent-child between two elements. A child combinator is made of the "greater-than (&gt;)” character and separates two elements.Examples: E &gt;
2 min read
How to make Parent Div Activate Styling of Child Div for Hover and Active ?
In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure
2 min read
Why margin on child element moves the parent element in CSS ?
In CSS, when a margin is applied to a child element, it can affect the position of the parent element. This happens because the margins of an element collapse with the margins of its adjacent siblings or its parent, based on certain rules. If the parent element has no padding or border, and the child element has a top margin, then the child element
3 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to remove parent element except its child element using jQuery ?
Given an HTML document and the task is to remove the parent element except for its child element. Approach 1: Use contents() method to select all the direct children, including text and comment nodes for the selected element.Selected elements are stored in a variable.Now, use replaceWith() method to replace the content of parent element by its all
2 min read
How to return true if the parent element contains the child element in JavaScript ?
In JavaScript, children property returns an array of those elements which are children of a given element. In order to check whether the parent element (say x) contains a child element (say y), we can use the children property to get the array of all children of x and then check whether the array obtained contains y. To get the child element of a p
2 min read
How to apply CSS in last child of parent using jQuery ?
In this article, we will see how to set styles in the last child of the parent element using jQuery. To set the style on the last child of parent, we use jQuery :last-child selector. The :last-child selector is used to select every element that is the last child of its parent element. Syntax: $("Selector:last-child") Approach: First, we create an H
1 min read
How to expand floated child div's height to its parent's height in CSS ?
Making the height of the floating child div equal to the height of the parent div is referred to as expanding the height of the floated child div to the height of the parent div. Approach 1: Using the CSS "clearfix" method is the first approach to increase a floated child div's height to match its parent's height. When an element is floated, it is
4 min read
How to get the child element of a parent using JavaScript ?
In this article, we will learn to get the child element of the parent using JavaScript. Given an HTML document, the task is to select a particular element and get all the child elements of the parent element with the help of JavaScript. There are 2 ways to get the child element: Table of Content By using the children propertyBy using the querySelec
3 min read