"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Darken Element Background Colors on Hover Without Affecting Transparency?

How to Darken Element Background Colors on Hover Without Affecting Transparency?

Published on 2024-11-16
Browse:148

How to Darken Element Background Colors on Hover Without Affecting Transparency?

Darkening Element Background Color with CSS

Enhancing the user interface involves highlighting interactive elements, such as buttons, by altering their appearance. A common approach is to darken the background color upon hover.

Initially, one might attempt to adjust the opacity, but that affects both the color and transparency. A more targeted solution exists.

Method: Overlay a Dark Layer

Create a dark overlay using background-image. This method preserves the original text color while darkening the background.

Implement the overlay layer in CSS:

.Button {
  background-image: linear-gradient(rgb(0 0 0/40%) 0 0);
}

.Button:hover {
  background-color: /* Original background color */;
}

This technique automagically darkens any background color, even multiple colors as seen in the example:

some text

By leveraging this overlay method, developers can easily darken element background colors on hover, enhancing user interactivity without the hassle of manually calculating darker shades.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3