Home > Web Front-end > CSS Tutorial > How can I dynamically adjust the brightness of a color using CSS?

How can I dynamically adjust the brightness of a color using CSS?

DDD
Release: 2024-12-07 05:18:14
Original
626 people have browsed it

How can I dynamically adjust the brightness of a color using CSS?

Dynamic Color Manipulation with CSS Filters

Question:

How can we dynamically lighten or darken a specified color using CSS? Can we reduce a color by a percentage?

Answer:

Yes, reducing a color by a percentage is possible using CSS filters. Here's how:

.button {
  color: #ff0000;
}

/* note: 100% is baseline so 85% is slightly darker, 
   20% would be significantly darker */
.button:hover {
  filter: brightness(85%);
}
Copy after login

In this example, the base color is set as red (#ff0000) for the button. By applying a filter to the button on hover, the brightness is reduced by 15%, resulting in a darker shade of red. Percentages can be adjusted to achieve desired levels of lightness or darkness.

This approach is compatible with all modern browsers, as evident from its widespread availability in the caniuse compatibility table.

The above is the detailed content of How can I dynamically adjust the brightness of a color using CSS?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template