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
Release: 2024-12-10 12:51:09
Original
929 people have browsed it

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;
}
Copy after login

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!

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