Home > Web Front-end > CSS Tutorial > Can CSS Change an Element's Properties on Hovering Over a Different Element?

Can CSS Change an Element's Properties on Hovering Over a Different Element?

Susan Sarandon
Release: 2024-12-10 10:54:18
Original
255 people have browsed it

Can CSS Change an Element's Properties on Hovering Over a Different Element?

Impacting a Distinct Element on the Hover of Another

Is it possible to alter an element's properties when a separate element is hovered over? This query delves into ways to achieve this behavior using only CSS.

While CSS's inherent capabilities prevent targeting non-descendants or adjacent siblings, there are specific scenarios where it's possible.

Descendants

If the affected element is a descendant of the hovered element, the CSS selector can effectively target it using:

#parent_element:hover #child_element, /* or */
#parent_element:hover > #child_element {
    opacity: 0.3;
}
Copy after login

For example, this targets the "child_element" within the "parent_element" on hover:

<div>
Copy after login
Copy after login

Adjacent Siblings

Targeting adjacent siblings requires a slightly different approach:

#first_sibling:hover + #second_sibling {
    opacity: 0.3;
}
Copy after login

This affects "second_sibling" when "first_sibling" is hovered over:

<div>
Copy after login
Copy after login

Implementation in Your Case

Considering your sample, you likely aim for something similar to:

img:hover + img {
    opacity: 0.3;
    color: red;
}
Copy after login

This affects the second image when the first image is hovered over.

Demo

Visit [this JS Fiddle demo](insert demo link here) for an interactive example.

The above is the detailed content of Can CSS Change an Element's Properties on Hovering Over a Different Element?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template