The Wayback Machine - https://web.archive.org/web/20240611175808/https://www.geeksforgeeks.org/how-to-affect-other-elements-when-one-element-is-hovered-in-css/
Open In App

How to affect other elements when one element is hovered in CSS ?

Last Updated : 20 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to affect other elements when one element has hovered using CSS. if we define 2 HTML elements where we want to hover over one element & at the same moment want to change the style of another element then both the elements should be directly related as either parent-child or sibling, which indicates either one element must be inside another element or both elements should be within the same target element so that the hover effect can be seen. Hover is used to add styling to the elements when the mouse hovers over them, i.e. when you place your cursor over the element then some text will appear which tells you about the functionality of that element. It is generally used for navigation when the UI of any website or software is complex.

Approach: This task can be accomplished by adding one element inside the other element & accordingly declaring the required CSS properties for the parent-child elements, so that whenever hovering outside the element then automatically the property of the inner element will change.

Example 1: In the below example, we will see how other element gets affected when we hover over one element.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
          
        .parent {
            width: 600px;
            height: 200px;
            background-color: lightgrey;
        }
          
        .child {
            margin-left: 45px;
            width: 100px;
            height: 30px;
            background-color: grey;
        }
          
        div {
            outline: 1px solid green;
        }
          
        .parent:hover .child {
            background-color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        Hovering one element to change 
        the effect to other element
    </h3>
    <div class="parent"> GeeksforGeeks
        <div class="child">
            GeeksforGeeks 
        </div>
        <br>
        <div class="child">
            GeeksforGeeks 
        </div>
        <br>
        <div class="child">
            GeeksforGeeks 
        </div>
    </div>
</body>
</html>


Explanation: In the above example, we have made two div elements named parent and child. If we hover over the parent element then automatically it will affect the child element.

Output: 

 

Example 2: In the below example, we will create two div elements. When you hover on one element then other elements will change their property.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
          
        .parent {
            width: 600px;
            height: 200px;
            background-color: lightgrey;
        }
          
        .child {
            width: 30px;
            height: 30px;
            background-color: grey;
        }
          
        div {
            outline: 1px solid black;
        }
          
        .parent:hover .child {
            background-color: green;
        }
          
        .child {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 50px 160px;
            margin-top: 50px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        Element's property will change when 
        hovering to other element
    </h3>
    <div class="parent">
        <button class="child">
            GeeksforGeeks 
        </button>
    </div>
</body>
</html>


Output:

 



Similar Reads

How to create Animated Hovered 3-D Buttons Effect using CSS ?
The hovered 3-D effect on a button is an effect in which the button appears to come toward the user on hovering. These buttons are created using HTML and CSS. Approach: The best way to animate the HTML objects is done by using CSS @keyframes and by setting the transitions at different stages. Example: In this example, we will create animated hovere
3 min read
How to Create Image Hovered Detail using HTML & CSS ?
In this article, we will learn how to create a type of hover effect over an image to get its complete detail. This is can be achieved by using simple HTML &amp; CSS. Overview Of Our Project: Approach: We will first create an HTML file in which we are going to add an image for our image hanger.We will then create a CSS style to give animation effect
3 min read
How to wrap an element with more content in flexbox container to have the same width as other elements in CSS ?
In a flexbox container, we might have a scenario where you want to wrap an element with more content, such as text or an image, to have the same width as other elements. In this article, we will see how to wrap an element with more content in a flexbox container to have the same width as other elements using CSS. The flexbox or flexible box model i
3 min read
Can a CSS class inherit one or more other classes ?
In this article, we will see how a class can inherit properties of other classes using CSS, along with understanding their basic implementations through the illustration. A descendent class can inherit any CSS property from the parent class by using Inheritance. Inheritance is the process by which genetic information is transmitted from parents to
4 min read
JavaScript Program to Find Element that Appears once in Array where Other Elements Appears Twice
Given an Array of Integers consisting of n elements where each element appears twice except one element. The task is to find the element which appears only once in the input array in JavaScript. Example: Input : arr = [ 5, 9, 2, 7, 4, 8, 6, 1, 5, 1, 4, 6, 3, 7, 8, 2, 9 ] Output : 3Explanation: The input array consists of 17 elements where all eleme
3 min read
How to target components around or within other elements in CSS ?
In this article, we will see how can we target components around or within other elements. This task can be achieved by using the target selector. The target selector in CSS is used to represent a unique element with an id attribute matching the URL’s fragment and It can be used to style the currently active target element. URLs with a # followed b
2 min read
JavaScript Auto-filling one field same as other
(This article takes some prior knowledge of HTML, CSS, and JavaScript.) You might have noticed that sometimes websites like e-commerce or some government website have two address fields in their forms. One for the primary address and another for the secondary address(or one for the billing address and another for the shipping address etc). Most of
3 min read
How to pass form variables from one page to other page in PHP ?
Form is an HTML element used to collect information from the user in a sequential and organized manner. This information can be sent to the back-end services if required by them, or it can also be stored in a database using DBMS like MySQL. Splitting a form into multiple steps or pages allow better data handling and layering of information. This ca
4 min read
How to fixed one column and scrollable other column or columns in Bootstrap ?
Bootstrap provides several built-in features to the users. This makes development faster and smoother. However, in some cases, external CSS and jQuery has to be implemented to the web page's coding in order to give it a personalized touch. Usually, the Bootstrap grid is used to divide the web-page into 12 equal columns. These columns can be grouped
4 min read
How to put two columns one below other in sidebar in Bootstrap ?
Developed by Mark Otto and Jacob Thornton, Bootstrap is a free and open-source responsive CSS framework used for front-end web development. Though Bootstrap 5 Alpha is the most recent version of Bootstrap launched on 16th June 2020, it is still in the development phase and hence most of the developers are continuing to use Bootstrap 4. Bootstrap is
5 min read