Home >Web Front-end >CSS Tutorial >How Can I Style a Parent Element on Child Hover Without Using Parent Selectors?

How Can I Style a Parent Element on Child Hover Without Using Parent Selectors?

Barbara Streisand
Barbara StreisandOriginal
2024-12-30 17:19:14840browse

How Can I Style a Parent Element on Child Hover Without Using Parent Selectors?

Styling Parent Elements on Child Hover without Parent Selectors

While CSS lacks a dedicated parent selector, it's still possible to style parenting elements when child elements are hovered. In this scenario, we aim to highlight a container element when the Delete button it contains is hovered over.

Consider the HTML markup:

<div>
    <p>Lorem ipsum ...</p>
    <button>Delete</button>
</div>

Without a parent selector, we can leverage the concept of pseudo-wrappers to achieve the desired effect. By setting pointer-events: none; on the parent element and pointer-events: auto; on a child element (a pseudo-wrapper), we can create a trigger mechanism.

Here's the CSS code to implement this:

div.parent {  
    pointer-events: none;
}

div.child {
    pointer-events: auto;
}

div.parent:hover {
    background: yellow;
}

Note: Ensure that child elements with their own event listeners have pointer-events set to auto to maintain click functionality.

The above is the detailed content of How Can I Style a Parent Element on Child Hover Without Using Parent Selectors?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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