Home >Web Front-end >CSS Tutorial >How Can I Dynamically Change Text Color Based on Background Color in a Progress Bar?

How Can I Dynamically Change Text Color Based on Background Color in a Progress Bar?

DDD
DDDOriginal
2024-12-10 12:51:09928browse

How Can I Dynamically Change Text Color Based on Background Color in a Progress Bar?

Text Blended Over Background Color

In an attempt to style a custom progress bar, a developer faces a challenge in changing the text color dynamically based on the background color underneath. The desired effect is a dark text over a light background and vice versa.

Various mix-blend-mode and color combinations have been tried unsuccessfully. An alternative solution Involves creating a separate gradient to color the text:

.container {
  background: linear-gradient(to right, #43a047 50%, #eee 0);
  text-align: center;
}

.text {
  background: linear-gradient(to right, white 50%, black 0);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  -webkit-text-fill-color: transparent;
  font-weight: bold;
}

By using a linear gradient on the text element, the color can transition smoothly from white to black based on the background gradient. The text becomes transparent and is filled with the desired gradient, resulting in the desired blended effect.

The above is the detailed content of How Can I Dynamically Change Text Color Based on Background Color in a Progress Bar?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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