


How to dynamically modify the hover color of HTML elements by clicking the button?
Apr 04, 2025 pm 03:42 PMUsing CSS variables and JavaScript to dynamically modify element hover color
This article introduces how to dynamically change the hover color of HTML elements by clicking a button to improve the web interactive experience. We will use CSS variables and JavaScript to implement this functionality.
The goal is: click the button to modify the existing element hover style. To do this, we will use CSS variables to define the hover color and modify the value of the CSS variable in the button click event through JavaScript.
Implementation plan:
First, the HTML structure contains the target element ( .element
) and multiple buttons ( .change-color
), each button corresponding to a hover color:
<div class="container"> <div class="element"></div> <button class="change-color">red</button> <button class="change-color">green</button> <button class="change-color">blue</button> </div>
CSS style defines the basic style of the element and uses the CSS variable --color
to control the hover color, default red:
.element { width: 100px; height: 100px; /* aspect-ratio is not compatible with all browsers, changed to fixed height*/ margin: 20px; background-color: lightgray; /* Add background color to make it easier to observe the effect*/ } .element:hover { background: var(--color, red); }
Finally, the JavaScript code listens for each button click event. After clicking the button, get the button text content (color name) and assign the color value to the CSS variable --color
using el.style.setProperty()
method:
const el = document.querySelector(".element"); document.querySelectorAll(".change-color").forEach(e => { e.addEventListener("click", () => { el.style.setProperty("--color", e.textContent); }); });
Through the collaborative work of HTML, CSS and JavaScript, you can realize the function of dynamically changing the hover color of HTML elements by clicking a button. This method is simple, efficient and easy to maintain.
The above is the detailed content of How to dynamically modify the hover color of HTML elements by clicking the button?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

The latest version of Binance is v2.102.5, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

The latest version of Binance is v2.102.5, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

The latest version of Binance is v2.102.5, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

How to conduct BTC transactions through Binance App? The answers are as follows: 1. Download and install the Binance App, complete registration and identity verification, and recharge funds; 2. Open the App to search for BTC, select trading pairs such as BTC/USDT, and be familiar with price charts and entrustment types; 3. Choose Buy or Sell, set limit orders or market orders and submit an order; 4. Check the order status on the entrustment page, view records through historical orders, and manage digital assets on the asset page.

The Virtual Currency Exchange APP is a professional digital asset trading application, providing users with safe and convenient digital currency trading services. The new v6.129.0 version has upgraded the performance and operation experience, aiming to bring a smoother trading experience.

The latest version of Binance is 2.101.8, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

The latest version of Binance is 2.101.8, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

The latest version of Binance is v2.102.5, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.
