Home > Web Front-end > CSS Tutorial > How to Change the Color of Sibling Elements on Hover with CSS?

How to Change the Color of Sibling Elements on Hover with CSS?

DDD
Release: 2024-12-10 08:07:16
Original
358 people have browsed it

How to Change the Color of Sibling Elements on Hover with CSS?

How to Selectively Change Color of Siblings with CSS Hover

When hovering over an element, there are certain CSS techniques that can be employed to alter the appearance of its neighboring elements.

Targeting Subsequent Siblings

Using a " " sign in your CSS selector allows you to target the element immediately following the element you're hovering over. For instance, the following code changes the color of an "a" element when the preceding "h1" element is hovered:

h1:hover + a { color: #4f4fd0; }
Copy after login

Targeting Previous Siblings

However, CSS has limitations when it comes to targeting previous siblings. To achieve a similar effect, you would need to wrap the elements within a parent container and use the general sibling selector "~" to target the desired element. However, this method may not always produce the desired result.

#banner h1:hover ~ a { color: #4f4fd0; }
Copy after login

Example Scenario

Consider the following HTML markup:

<h1>
Copy after login

To change the color of the "a" element when the "h1" with the id "title" is hovered, you can use the following CSS:

#title:hover + .button { color: #4f4fd0; }
Copy after login

The above is the detailed content of How to Change the Color of Sibling Elements on Hover with CSS?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template