Home > Web Front-end > CSS Tutorial > How to Apply Hover Effects to Pseudo Elements Triggered by Another Element?

How to Apply Hover Effects to Pseudo Elements Triggered by Another Element?

Patricia Arquette
Release: 2024-11-19 14:11:02
Original
374 people have browsed it

How to Apply Hover Effects to Pseudo Elements Triggered by Another Element?

Adding Hover Effect to Pseudo Elements

Question:

How can one implement a hover effect on a pseudo element, such as #element:before:hover, and make that hover triggered by hovering over another element, like #button:hover #element:before {...}?

CSS Only Method:

Yes, it's possible to achieve this purely with CSS:

#button:before {
    background-color: blue;
    content: "";
    display: block;
    height: 25px;
    width: 25px;
}

#button:hover:before {
    background-color: red;
}
Copy after login

jQuery Method:

If you prefer jQuery, here's a solution:

<div class="snippet" data-lang="js" data-hide="true">
<div class="snippet-code snippet-currently-hidden">
<pre class="snippet-code-css lang-css prettyprint-override">#button {
    display: block;
    height: 25px;
    margin: 0 10px;
    padding: 10px;
    text-indent: 20px;
    width: 12%;}

#button:before { background-color: blue;
    content: "";
    display: block;
    height: 25px;
    width: 25px;}

#button:hover:before { background-color: red;}
<div>
Copy after login
$(function() {
    $("#button").hover(
        function () {
            $(this).find(":before").css("background-color", "red");
        },
        function () {
            $(this).find(":before").css("background-color", "blue");
        }
    );
});
Copy after login

The above is the detailed content of How to Apply Hover Effects to Pseudo Elements Triggered by Another 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