Home > Article > Web Front-end > How Can I Darken an Element's Background on Hover Without Finding New Hex Codes?
When seeking to slightly darken an element's background color on hover, developers often resort to painstakingly finding darker hex codes that align with the original color. However, a more expeditious solution emerges from CSS's ability to utilize a translucent overlay.
By incorporating a semi-transparent dark layer atop the existing background, you can effortlessly achieve the desired darkening effect. This method enables you to maintain the text's original color while seamlessly transitioning the background's appearance.
To illustrate this technique, consider the following CSS code:
.button { display: inline-block; color: #fff; padding: 10px 20px; font-size: 20px; background-color: red; } .button:hover { background-image: linear-gradient(rgb(0 0 0/40%) 0 0); }
In this snippet, the button's hover state features a linear gradient that superposes the background color with a 40% opaque black layer. The result is a tasteful and subtly darker background effect that adapts seamlessly to different background colors.
<div class="button"> some text </div> <div class="button">
The above is the detailed content of How Can I Darken an Element's Background on Hover Without Finding New Hex Codes?. For more information, please follow other related articles on the PHP Chinese website!