Using CSS to Modify Class Style on Hover
Can a single CSS rule be used to alter the CSS properties of one class when hovering over an element from a separate class? Is it possible to create a rule such as:
.item:hover .wrapper { /*some css*/ }
where .wrapper is not directly contained within .item, but rather located elsewhere in the document?
CSS Solution
Yes, this is possible using CSS selectors. There are two approaches you can take when using :hover to modify the style of another element:
1. Descendant or Child Element
If .wrapper is a child element of .item, you can use this selector:
.item:hover .wrapper { /* CSS properties to change */ }
2. Sibling Element
If .wrapper is a sibling element of .item, appearing after .item in the DOM, you can use this selector:
.item:hover ~ .wrapper { /* CSS properties to change */ }
JavaScript Solution
While JavaScript is not necessary for this simple task, you can use it to achieve the desired effect. Here is an example:
document.getElementsByClassName('item')[0].addEventListener('mouseover', function() { document.getElementsByClassName('wrapper')[0].style.background = "url('some url')"; });
Additional Information
It's important to note that your example menu elements appear to be using distinct classes. When hovering over an element, its submenu appears to the right as an overlay. The class controlling the background of the submenu is named "wrapper." To achieve desired effect, use the sibling selector approach described above.
References
- [CSS 2.1 selectors](https://www.w3.org/TR/CSS21/selectors.html)
The above is the detailed content of Can CSS Modify One Class\' Style on Hovering Over Another?. For more information, please follow other related articles on the PHP Chinese website!

There's been a run of tools, articles, and resources about color lately. Please allow me to close a few tabs by rounding them up here for your enjoyment.

Robin has covered this before, but I've heard some confusion about it in the past few weeks and saw another person take a stab at explaining it, and I wanted

I absolutely love the design of the Sandwich site. Among many beautiful features are these headlines with rainbow underlines that move as you scroll. It's not

Many popular resume designs are making the most of the available page space by laying sections out in a grid shape. Let’s use CSS Grid to create a layout that

Page reloads are a thing. Sometimes we refresh a page when we think it’s unresponsive, or believe that new content is available. Sometimes we’re just mad at

There is very little guidance on how to organize front-end applications in the world of React. (Just move files around until it “feels right,” lol). The truth

Most of the time you don’t really care about whether a user is actively engaged or temporarily inactive on your application. Inactive, meaning, perhaps they

Wufoo has always been great with integrations. They have integrations with specific apps, like Campaign Monitor, Mailchimp, and Typekit, but they also


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft