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

How Can I Dynamically Adjust Text Color Based on a Gradient Background in CSS?

Susan Sarandon
Release: 2024-12-17 06:03:26
Original
606 people have browsed it

How Can I Dynamically Adjust Text Color Based on a Gradient Background in CSS?

Text Blending Over Background Color

You aim to style a progress bar with varying left-hand colors, and the text's color should adjust dynamically based on the underlying background.

Mix-Blend-Mode Attempt

You initially experimented with mix-blend-mode: difference to blend the text color with the background, but it proved unsuccessful. You also considered using filter: grayscale(1) contrast(9);, but that didn't yield the desired effect either.

Gradient Solution

An effective solution lies in creating an additional gradient to color the text. By employing this method, you eliminate the need for mix-blend mode.

The custom CSS code achieves this gradient override:

.container {
  width: 200px;
  height: 20px;
  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

This approach ensures that the text color transitions between black (in darker left-hand sections) and white (in lighter left-hand sections).

The above is the detailed content of How Can I Dynamically Adjust Text Color Based on a Gradient Background in 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template