CSS alone has the power to make multiple elements react to a hover action on a div

Is there a way to dynamically change the size of multiple div elements on hover? In this example, we can see how one div element's size changes when hovering over another div element: https://jsfiddle.net/9510a6kj/

 .left, .right{
        float:left;
    }

    .left{
        background: red;
        width: 200px;
        height: 300px;
        transition: width 1s ;
    }

    .right{
        background: blue;
        width: 300px;
        height: 300px;
        transition: width 1s;
    }

    .left:hover{
        width: 300px;
    }

    .left:hover + .right{
        width: 100px;
    }
    </style>

However, I'm curious if it's possible to achieve a similar effect where hovering over one div element ("a") triggers a size change in two other div elements ("b" and "c").

Your insights are greatly appreciated.

Answer №1

Achieving this effect is possible using the sibling selector. The adjacent sibling selector you applied in your code snippet will style only the element immediately following it in the DOM, whereas the sibling selector will style all sibling elements after the specified reference element .a.

Here's an example of HTML structure:

<div class="parent">
   <div class="a"></div>
   <div class="b"></div>
   <div class="c"></div>
</div>

Corresponding CSS styling:

.a:hover ~ div{
 //apply styles to .b and .c here
}

It is worth noting that the sibling selector only affects siblings that come after the reference element...it does not target any siblings positioned before it. At present, CSS lacks the capability to traverse upwards in the DOM tree.

Answer №2

The relationship between these elements determines the CSS selectors to use.

1. If they are siblings and adjacent, you can use + for the c div as well. Check out the example below:

div {
  height: 50px;
  width: 50px;
  border: 1px solid black
}

.a:hover + .b {
  background: blue;
}

.a:hover + .b + .c {
  background: red;
}
<div class="a">
  a
</div>
<div class="b">
  b
</div>
<div class="c">
  c
</div>

2. If they are siblings but not adjacent, use ~ for both b and c after a.

div {
  height: 50px;
  width: 50px;
  border: 1px solid black
}

.a:hover ~ .b {
  background: blue;
}

.a:hover ~ .c {
  background: red;
}
<div class="a">
  a
</div>
<div>

</div>
<div class="b">
  b
</div>
<div>

</div>
<div class="c">
  c
</div>

3. When b and c come after a, with c immediately following b, combine ~ with +. See the code snippet below:

div {
  height: 50px;
  width: 50px;
  border: 1px solid black
}

.a:hover ~ .b {
  background: blue;
}

.a:hover ~ .b + .c {
  background: red;
}
<div class="a">
  a
</div>
<div>

</div>
<div class="b">
  b
</div>
<div class="c">
  c
</div>

  1. If they are children, you can use a:hover b,a:hover c or > (direct child) along with the sibling selectors mentioned above.

There are various possibilities depending on the structure of your HTML. Keep in mind that this can only be achieved in CSS if b and c follow a in the DOM tree.

Answer №3

Indeed, utilizing sibling selectors (as long as they are siblings)

.a {
  display: inline-block;
  width: 100px;
  height: 50px;
  background-color: red;
}

.b {
  display: inline-block;
  width: 100px;
  height: 30px;
  background-color: blue;
  transition: all 1s;    }

.c {
  display: inline-block;
  width: 100px;
  height: 80px;
  background-color: yellow;
  transition: all 1s;
}

.a:hover~.b {
  height: 160px;
}

.a:hover~.c {
  height: 120px;
}
<div class="a">hover me</div>
<div class="b">b b b b b</div>
<div class="c">c c c c c</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

Is the dropping of the middle section in the inline list imminent?

After creating a fiddle to check for conflicts with other styles on my page, I ran into an issue with an inline list. There seems to be a slight drop in the middle item's positioning that I can't figure out. Despite trying various margin adjustme ...

Enhancing the appearance of individual cells within an HTML table by applying custom classes

I'm in the process of automating report generation for my organization. Our workflow involves using R to summarize data from Excel, then utilizing Rmarkdown, knitr, and the "htmlTable" package to generate HTML files. Currently, I am implementing CSS ...

Include a class for CSS styling in EJS using Node.js

Attention: Please be aware of the environment in this Application. The following technologies are being used: Windows NPM, Express MySQL This is the code in an ejs template called example.ejs <% if (message) { %> <p class="al ...

Utilizing window.matchMedia in Javascript to retain user selections during page transitions

I am encountering difficulties with the prefers-color-scheme feature and the logic I am attempting to implement. Specifically, I have a toggle on my website that allows users to override their preferred color scheme (black or light mode). However, I am fac ...

Utilize JavaScript to trigger a div pop-up directly beneath the input field

Here is the input box code: <input type='text' size='2' name='action_qty' onmouseup='showHideChangePopUp()'> Along with the pop-up div code: <div id='div_change_qty' name='div_change_qty&ap ...

No code is appearing on the page, just a blank space

Whenever I visit this page on the web, the screen shows up as empty and I've encountered similar issues with other JavaScript pages that I've created. This makes me wonder if there might be a missing piece of code or something else causing the pr ...

Generate a custom image using a pre-existing background image and text using JavaScript, then save it as a JPEG file in

Managing multiple fanpages on Facebook can be quite a task, especially when it comes to posting images with funny text. I have different backgrounds for each page, which means I have to save each image individually with the text. To streamline this process ...

Curved base of the main banner image

Struggling to achieve the perfect curve at the bottom of my hero image. Any suggestions on how to accomplish this would be greatly appreciated. I am aiming for something like this: https://i.stack.imgur.com/Emxfc.png body { margin: 0; background: lig ...

Looking to alter the CSS of an ID element when hovering over a link on your website?

Irrespective of the positioning of the links in the html, a simple hover effect can trigger changes like switching images or altering backgrounds anywhere on the website. The ideal solution would involve a straightforward method without the need for Javas ...

Customizing Background Image Opacity in MuiCssBaseline

I recently tried to set a background image for my demo application built with React, Next, and Material-UI. In my _app.js file, I included the following code: import React from 'react'; import { ThemeProvider } from '@material-ui/core/styles ...

Sticky CSS - A guide to making aside elements fill the entire available height

How can I create a grid layout where the aside is sticky to top: 0 and fills all remaining height (e.g. 100% of height), but when scrolling down, the aside height should be changed to 100% minus the footer's height? Is it possible to achieve this with ...

What is the secret to aligning two elements flawlessly?

I'm currently working on completing a theme and I've hit a roadblock. I am trying to add header support, but it's causing issues with my layout. Everything was running smoothly until I inserted this line of code: <img src="<?php heade ...

There appears to be an error in the @media CSS code that needs to be corrected

Attempting to use an @media query to adjust the height of an element on my website for mobile users. However, I am struggling to make the code work properly. Any assistance in understanding what mistake I'm making and guiding me in the correct directi ...

Guidance on implementing fallback font formats using FontFace API

I am exploring the FontFace API (not @fontface) and wondering if there is an easy way to include multiple font formats, similar to providing multiple sources in @fontface. Alternatively, is there a simple method to identify which font formats are supporte ...

The Bootstrap menu does not seem to be affecting the positioning of the body text

I recently came across an issue with a Bootstrap-based site I'm working on. When I click the hamburger menu on mobile view, the main content doesn't shift down to accommodate it, causing an overlap. Not ideal :-( Here's my Demo: https://js ...

The checkbox filter in Angular 6 has the ability to replace the previously selected

I've been working on creating a filter system using checkboxes. Below is a snippet of the code I'm currently using: filter.pipe.ts import { Pipe, PipeTransform, Injectable } from '@angular/core'; @Pipe({ name: 'filter2' }) ...

Alter the color of a single character using JQuery, all while keeping the HTML tags unchanged

I'm currently working on iterating through the DOM using JQuery in order to change the color of every occurrence of the letter m. Here is what I have so far: $(document).ready(function(){ var m = "<span style='color: red;'>m</span& ...

Add the cordova plugin multi-image-picker with the following command

onDeviceReady: function() { window.imagePicker.getPictures( function(results) { for (var i = 0; i < results.length; i++) { alert('Image URI: ' + results[i]); } }, function ...

What steps should I take to make my Twitter widget adaptable to varying screen heights?

I'm currently utilizing react-twitter-widgets to incorporate a Twitter timeline within my application. While everything is rendering correctly, the height of the timeline extends down the entire page. Is there a way to adjust the widget's height ...

Strategies for transferring user input data to a function in view.py

Purpose: ** The goal is to pass the input value itemnumbervalue to the function itemnumber() in views.py > Issue encountered: The method object is not subscriptable at line 17 in view.py Attempted solutions: Case 1: Tried using both [] and () bra ...