


How to dynamically modify the hover color of HTML elements by clicking the button?
Using 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

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

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

Undress AI Tool
Undress images for free

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

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

JDBC...

The top ten safe and reliable exchanges in the 2025 cryptocurrency circle include: 1. Binance, 2. OKX, 3. Gate.io (Sesame Open), 4. Coinbase, 5. Kraken, 6. Huobi Global, 7. Gemini, 8. Crypto.com, 9. Bitfinex, 10. KuCoin. These exchanges are rated as safe and reliable based on compliance, technical strength and user feedback.

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...
