Change the color of divb using CSS when diva is hovered over

Is it possible to change the color of the second class to white when the first class is hovered over?

<div class="diva">This is the first class</div>

<div class="divb">This is the second class</div>

<style>
.diva:hover + .divb {
  color:#fff;
}
</style>

I've tried the above code but it doesn't seem to work. Any suggestions on how to make it work?

Answer №1

If you want to create a hover effect for another class in your CSS, you can use the CSS 3 general sibling combinator ~.

The elements [E1 ~ E2] represented by the two sequences have the same parent in the document tree, with the element represented by the first sequence [E1] appearing before the element represented by the second one [E2], not necessarily immediately.

The code below demonstrates how to set the font color to white for divb when the user hovers over diva. This combinator is useful when elements are not in adjacent positions.

<html>
   <body>
      <div class="diva">Hover over the first element</div>
      <div class="divb">I will change my color</div>
      <style>
         .diva:hover ~ .divb {
         color: #fff;
         }
      </style>
</body>

Answer №2

If you are searching for the adjacent sibling selector in CSS 2.1, it is denoted by the + symbol. As per the specifications:

The + selector identifies a match when E1 and E2 are siblings within the same parent in the document tree, with E1 immediately preceding E2.

.diva:hover + .divb {
  color: #fff;
  background-color: green;
  padding-left: 4em;
}
<div class="diva">Hover over the first div to change the color of its sibling</div>
<div class="divb">This is the second div</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

Issues with sending form data to controller using AJAX in Laravel

I'm currently working on a form in Laravel that enables users to purchase products dynamically without needing to refresh the page. While I am successfully able to initiate the AJAX function, I am encountering an issue where the data is not being post ...

Creating a customized file input using CSS

I am currently utilizing Bootstrap4 to craft my webpage and I am incorporating a custom file input bar. To retrieve the three labels of the input bar ("Choose file", "Browse", "Upload") from my stylesheet, I have created three custom classes with their co ...

Every time I click, the click event is getting attached repeatedly through the on method

Here is the HTML code that I am working with: <div class="connect"> <ul> <li><a href="#">Assign a Task</a></li> <li><a attr="viewCard" href="#">View Ca ...

It is not possible to attach separate links to images in a JavaScript slider

I am struggling with linking individual images in a JavaScript slider for a client's website. The slider is fully functional and works well, but I can't seem to figure out how to link the images properly. When I remove items from <--ul class= ...

CSS and JavaScript dropdown malfunctioning due to a position swap in internal CSS

This example demonstrates functionality .c1 { background-color:red; position:relative; } .c2 { background-color:blue; position:absolute; height:50px; width:100px; display:none;} .show { display:block; } <body> <button ...

Obtain specific text from elements on a website

How can I extract a text-only content from a td tag like 'Zubr Polish Lager 6%'? <td width="35%">Amber Storm Scotch Ale 6% <br/>SIZE/LIFE: 330ml <b>CASE</b> <br/>UOS ...

Calculation for determining the fill of a DIV's remaining height

Here is what I have: <div id="modal-container"> <div id="modal-body"></div> <div id="modal-footer"></div> </div> I am currently working on a JavaScript function to adjust the height of #modal-body to fill in the re ...

Choosing the Right Project for Developing HTML/Javascript Applications in Eclipse

Whenever I attempt to build a webpage using eclipse, I am presented with two choices: -- A Javascript project -- A Static web project If I opt for the former, setting up run to open a web browser can be quite challenging. If I decide on the latter ...

Establish a buffering system for the <video> element

Currently, I am facing an issue with playing videos from a remote server as they take an extended amount of time to start. It appears that the entire video must be downloaded before playback begins. Is there a way to configure the videos so they can begi ...

Parsing HTML to access inner content

Currently, I have integrated an onClick event to an anchor tag. When the user interacts with it, my objective is to retrieve the inner HTML without relying on the id attribute. Below is the code snippet that illustrates my approach. Any assistance in acc ...

Displaying the usernames of the users on the interface leads to encountering the 'Undefined Index' error

Hey there, I'm currently facing an issue with displaying the user's first name as a greeting message on index.php. I want it to say 'Hello, (user's first name)', but since I'm fairly new to PHP, I'm struggling to make it ...

Whenever the download bar emerges at the bottom, the slideshow content on the page suddenly shifts upwards

Each time the download bar shows up at the bottom, the slideshow content on the Homepage suddenly moves up. It returns to its original position after I close the download bar.https://photos.app.goo.gl/F482eMfkXyfEZkdA9. My assumption is that this issue is ...

Creating an engaging Uikit modal in Joomla to captivate your audience

I need help optimizing my modal setup. Currently, I have a modal that displays articles using an iframe, but there is some lag when switching between articles. Here is the JavaScript function I am using: function switchTitleMod1(title,id) { document.g ...

Animate the Bootstrap carousel from top to bottom instead of the traditional left to right movement

Is there an option available in the bootstrap carousel to animate it from top to bottom and bottom to top, rather than from right to left and left to right? jsFiddle link <div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval=" ...

Fluid and static containers in Bootstrap offer flexible and fixed-width layout

I am experimenting with combining fluid containers and fixed containers within a single page layout using Bootstrap. For example, I want to have a large image as a hero unit that spans 100% of the width and height of the viewport, or three column images e ...

What is the best way to add child elements to the parent element when the height limit has been reached?

This is the code I've been working on: <ul style="height:200px;width:100%;"> <li>Hi</li> <li>Hi</li> <li>Hi</li> ... (add more items here) </ul> Here is a preview of how it currently looks: ...

Choose an image and save the selection information for the following page (Tarot card)

I'm in the process of creating a website that showcases multiple tarot cards. The goal is for users to select the cards they're interested in and have their chosen card displayed on the next page. I've implemented some code for selecting the ...

One source of HTML content for a div element across multiple webpages

Could you please provide guidance on where to start investigating or researching for this issue? I am interested in having a single source for the content of a <div>, which can be used across multiple pages. This way, I can update one source and hav ...

Ways to refresh a webxr session while clearing all models from the scene

Whenever the webxr session restarts, I notice that two instances of previous objects appear on the screen. I want the screen to be clear when the session restarts. Currently, I am using the following code: for( var i = scene.children.length - 1; i >= 0 ...

html body extends beyond the negative position of the element

If you visit my website at this link, try resizing the window until the responsive design kicks in. Then, click on the "right" button at the top of the page. You'll notice that the right sidebar disappears and reappears, but when it disappears, the ba ...