The Wayback Machine - https://web.archive.org/web/20240618200141/https://www.geeksforgeeks.org/how-to-make-parent-div-activate-styling-of-child-div-for-hover-and-active/
Open In App

How to make Parent Div Activate Styling of Child Div for Hover and Active ?

Last Updated : 18 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 active

  • Using Pure CSS: with pseudo-classes
  • Using CSS and JavaScript: without pseudo-classes

Method 1: Using Pure CSS

Pseudo-class

Pseudo-classes are keywords used in CSS to define special states of HTML elements based on user interactions or element states without the need for additional classes or JavaScript.

  • hover: This pseudo-class targets an element when the user hovers their mouse pointer over it.
  • active: This pseudo-class targets an element when it’s being activated, usually by a user clicking on it.

Example: In this example, we will use CSS pseudo-selectors to make parent div activate styling of child div for hover and active.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        .parent-div {
          width: 200px;
          height: 200px;
          background-color: lightgray;
        }
          
        .child-div {
          width: 100px;
          height: 100px;
          background-color: skyblue;
          margin: 50px;
          transition: background-color 0.3s;
        }
          
        .parent-div:hover .child-div {
          background-color: lightgreen;
        }
          
        .parent-div:active .child-div {
          background-color: coral;
        }
    </style>
</head>
  
<body>
    <div class="parent-div">
        <div class="child-div">Hover and Click Me</div>
    </div>
</body>
  
</html>


Output:

Peek-2023-08-14-12-15

Method 2: Using CSS and JavaScript

Example: In this article, we will use CSS and JavaScript to make parent div activate styling of child div for hover and active.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        .parent-div {
          width: 200px;
          height: 200px;
          background-color: lightgray;
        }
          
        .child-div {
          width: 100px;
          height: 100px;
          background-color: skyblue;
          margin: 50px;
        }
          
        .child-div.active {
          background-color: coral !important;
        }
    </style>
</head>
  
<body>
    <div class="parent-div">
        <div class="child-div">
              Hover and Click Me
          </div>
    </div>
  
    <script>
        const parentDiv = 
                  document.querySelector('.parent-div');
        const childDiv = 
                  parentDiv.querySelector('.child-div');
          
        parentDiv.addEventListener('click', () => {
              childDiv.classList.toggle('active');
        });
          
        parentDiv.addEventListener('mouseover', () => {
              childDiv.style.backgroundColor = 'lightgreen';
        });
          
        parentDiv.addEventListener('mouseout', () => {
              childDiv.style.backgroundColor = 'skyblue';
        });
          
    </script>
  
</body>
  
</html>


Output:

Peek-2023-08-14-12-15



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 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 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
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 clear all div's content inside a parent div ?
In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To accomplish this task, we can use the following methods: Table of Content jQuery remove() methodjQuery empty() methodjQuery html() methodjQuery remove() methodThe remove() method in jQuery is used to remove the
2 min read
Styling Active Router Link in Angular
Styling the active router link in angular allows the user to differentiate between the active router link and inactive router links. Angular provides a special mechanism to work with active router links. Approach: Create the Angular app to be used.Create the header component that contains the navigation links.Then apply the "routerLinkActive" on ea
2 min read
How to override React Bootstrap active Tab default border styling?
React-Bootstrap provides various components with basic default styling. Sometimes, there is a need to override those basic default styles as per requirements. In this article, let us discuss overriding that default styling for the border of the active tab in the navbar. PrerequisitesReact JSReact-BootstrapSteps to create React Application and insta
3 min read
How To Add Styling To An Active Link In NextJS?
NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about how to add styling to an active link in NextJS. Approach to Add Styling To An Active Link I
2 min read
How to add &lt;li&gt; class to active and leave it after hover using jQuery ?
Bootstrap 4 is an open-source CSS framework used for frontend development of web applications. Bootstrap along with HTML and Javascript enrich the user interface for better user experience. jQuery is a Javascript framework that is used to execute Javascript functions. jQuery has similar functionalities like javascript but the only difference is jQu
4 min read
How to use hover, focus and active variants in Tailwind CSS ?
In this article, we will see how to use hover, focus, and active variants in Tailwind CSS. Tailwind CSS uses the Hover, Focus, and Active variants to style an element when the user mouses move over it, focuses it, or actively clicks/tapped it. These variants allow you to create interactive and dynamic user interfaces without writing custom CSS. The
4 min read