Is there a way to modify CSS styles for a child tag element after hovering over its parent?

Here are my HTML code segments:

<div class="div-builder">
    <div>
        <a href="#">
            <img src="/photos/20/Apple-Logo-icon.png" width="50" alt="Logo">
        </a>
        <h3>Title</h3>
        <i>sub-title</i>
    </div>
    <hr/>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting ...</p>
</div>

I want to create a hover effect for the .div-builder class where its background color changes according to this CSS:

.div-builder{ background-color: lightyellow; }

The desired result should look like the image shown in this link: https://i.sstatic.net/U1PRe.jpg

In addition, when hovering over the <img> tag, I also want its background color to change to lightyellow without affecting the .div-builder class. The result should be similar to the image in this link: https://i.sstatic.net/PXt5E.jpg

The <img> tag can be at any level within the .div-builder class. Similarly, the <h3>, <i>, or <p> tag should change their background color to lightyellow on hover, with the .div-builder class background color remaining unchanged. After hovering over the <p> tag, the result should resemble the image here: https://i.sstatic.net/KaXes.jpg

Is there a way to achieve this hover effect using CSS?

Alternatively, how can I implement this click event instead of a hover effect using jQuery?

Answer №1

Hey there! Check out this answer I put together for you. Here is the link

function hoverEffect() {
      $(".div-builder")
        .find("h3,i,p,img,a")
        .hover(
          function () {
            $(this).css("background", "SpringGreen");
            $(".div-builder").css("background", "initial");
          },
          function () {
            $(this).css("background", "initial");
            $(".div-builder").css("background", "SpringGreen");
          }
        );
    }
    function resetHover() {
      $(".div-builder").css("background", "initial");
    }
<div class="div-builder" onmouseenter="hoverEffect()" onmouseleave="resetHover()">
      <div>
        <a href="#" width="100%">
          <img src="https://picsum.photos/id/4/300" alt="Logo">
        </a>
        <h3 class="">Title</h3>
        <i>sub-title</i>
      </div>
      <hr />
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting ...</p>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Answer №2

Is this the style you're looking for?

body {
    background-color: rgba(173, 173, 173, 0.9);
}


.div-builder {
    background: white;
}

.div-builder div:hover {
    background: red;
}
    <div class="div-builder">
        <div>
            <a href="#">
                <img src="https://static.javatpoint.com/htmlpages/images/html-tutorial.png" alt="Logo">
            </a>
        </div>
        <div>
            <h3>Title</h3>
        </div>

    <div>
         <i>sub-title</i>    
    </div>
        <hr/>
        <div>
            <p>Lorem Ispsum is simply dummy text of the printing and typesetting ...</p>
        </div>
    </div>

Answer №3

If I understand correctly, your objective is to change the background of an element when hovering over it with the cursor. Achieving this is a simple task: It can be done using CSS by adding ":hover" to the CSS properties. For example, for a paragraph (p):

 p:hover {
      background-color: blue;
      font-size: 18px;
 }

Done! You can change the "p" selector to any ID or class you prefer.

See an example of changing background on hover. I hope this explanation was helpful.

If you want to change multiple elements at once when hovering over one:

.hoverThis:hover { background: green; }

Wrap the elements with a div container:

<div  class="hoverThis">
<p>This text will have a green background when hovered over.</p>
<p>And so will this one!</p>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS: Utilizing ellipsis for text overflow and optimization

As a beginner in angularJS, I might have made some mistakes... Can anyone guide me on how to properly implement this plugin: https://github.com/BeSite/jQuery.dotdotdot on my table? Currently, the performance of my edit form and table is very slow. What c ...

Learn how to implement JavaScript code that allows a video to start playing only upon clicking a specific button

Within the confines of a div lies a <video autoplay> <source src='myvid'> </video>. This div initially has a display ='none' setting in its CSS. Upon receiving a click event, the display property changes from none to b ...

After the successful completion of the AJAX request, I aim to transmit the identification data via the

Click here for image preview Below is the table content: Using jQuery: <script type="text/javascript> $(document).ready(function(){ $(document).on('keydown','#search',function(){ var input = $(this).val(); ...

Is it possible to dynamically change an ngModel value directly from the component?

I'm currently immersed in an Angular project and my initial file setup was like this: dog.ts: export interface Dog { name: string; age: number; breed: string; } dog.component.ts: import { Dog } from '../dog'; @Component({ //setup ...

Transforming the appearance of the menu element in Vue using transitions

In my Vue app, I have this SCSS code that I'm using to create a smooth transition effect from the left for a menu when the isVisible property is set to true. However, I am encountering an issue where the transition defined does not apply and the menu ...

display information in a structured table format

I am in need of HTML code that will render headers aligned above corresponding values when displayed. header1 header2 header3 List item 1 value111 value112 value113 value121 valueb122 valueb123 List item 2 value211 value212 value213 value221 valueb2 ...

Article discussing the Facebook like button

I've recently incorporated Facebook like buttons into the articles on my online store. It's great because when someone shares a post about a product or article, it shows up on their wall. Users receive notifications on Facebook when someone else ...

Show images dynamically with the help of Flask

Imagine having a camera that can capture real-time images and save them to a folder named "./static". The goal is to showcase each processed image on a local web page using Flask in Python. This means that the system user will be able to view the results ...

What could be causing the issue with my Date and Time input not functioning correctly?

I developed a React frontend application styled with Material UI design. The issue I am facing is that after adding the Date and Time component styling to the form, it is unable to process the "name" value for posting. Specifically, the error message "Ty ...

Navigation that sticks and changes upon hovering over div elements

Just delving into the world of jQuery and JS, so I appreciate your patience:) Currently, I have a sticky navigation bar positioned at the top of my webpage that links to different sections of content below. I am looking to create an effect where the corr ...

Internet Explorer 11's default document view mode now automatically switches to Edge viewing

I currently have Windows 7 and IE 11 installed on my local machine as I work on an ASP.NET, C# web application that utilizes Bootstrap, jQuery, and more. Recently, I implemented a checkbox dropdown list using the code from this link: . However, when view ...

The value entered is displaying as not defined

As a newcomer to the world of javascript, I am diving into creating a simple To Do list. The basic functionality is there, but I'm scratching my head trying to figure out why the input value in my code keeps returning undefined. The remove button is ...

Having trouble accessing the iframe element in an Angular controller through a directive

My webpage contains an iframe with a frequently changing ng-src attribute. I need to execute a function in my controller each time the iframe's src changes, but only after the iframe is fully loaded. Additionally, I require the iframe DOM element to b ...

How can you load elements via AJAX?

Using AJAX, I successfully load content from a PHP page. Now, my challenge is to access and interact with the dynamically loaded elements (such as buttons, forms, or divs). The code snippet that is causing issues: jQuery(document).ready(function() { $( ...

JavaScript: How can I remove it when I click anywhere on the webpage with onClick?

A challenge I'm facing involves creating a form with an input field that changes its border color when clicked. My goal is for the border to return to its original color when clicking anywhere else on the page. -HTML <form action=""> ...

Is there a way to make my for loop search for the specific element id that I have clicked on?

When trying to display specific information from just one array, I'm facing an issue where the for loop is also iterating through other arrays and displaying their names. Is there a way to only show data from the intended array? const link = document. ...

Modify an HTML table and record any modifications as POST requests using Javascript

As a beginner in JavaScript and HTML, I am open to corrections if I am not following the correct practices. Recently, I delved into editing an HTML form table. {%extends 'base.html'%} {%block content%} <html> <body> <h4> Search ...

Opacity of background color only applies to the color

So, here's the scenario - I currently have a background that looks like this: background:url(../img/arrow_lch.png) right no-repeat #2e443a; The challenge I'm facing is how to decrease the opacity of only the background color (#2e443a). Any sugg ...

Issue at hand: Unexpected error 500 encountered while sending AJAX request to PHP script through

UPDATE: Issue Resolved! I want to extend my gratitude to everyone who directed me to the error log files for assistance. The community here is truly incredible and I was able to get a resolution much quicker than anticipated. It's quite embarrassing, ...

Refresh a separate table following an insertion by utilizing a jQuery button coupled with PHP 7

This is a snippet of my jQuery code that inserts data into the database when a button is clicked $(function(){ $(document).on('click', '#addRefBtn', function(e){ var input = $('#qr_ref').val(); var po = $('#get_po& ...