Home > Web Front-end > CSS Tutorial > How to Invert CSS Font Color Based on Background Color?

How to Invert CSS Font Color Based on Background Color?

Susan Sarandon
Release: 2024-12-24 20:52:11
Original
540 people have browsed it

How to Invert CSS Font Color Based on Background Color?

Invert CSS Font Color Based on Background Color

Achieving font color inversion based on background color is a common design requirement. This question explores how to accomplish this using CSS.

Solution: Mix-Blend-Mode (Limited Support)

The mix-blend-mode CSS property allows color mixing based on the underlying background color. However, this property is not supported in Internet Explorer.

Solution: Pseudo Elements (Recommended)

The recommended approach for cross-browser compatibility is to use pseudo elements (:before and :after). By placing a colored pseudo element over the text and setting its text color to the desired inverse value, you can create the desired effect.

.inverted-bar {
  position: relative;
}

.inverted-bar:before,
.inverted-bar:after {
  padding: 10px 0;
  text-indent: 10px;
  position: absolute;
  white-space: nowrap;
  overflow: hidden;
  content: attr(data-content);
}

.inverted-bar:before {
  background-color: aqua;
  color: red;
  width: 100%;
}

.inverted-bar:after {
  background-color: red;
  color: aqua;
  width: 20%;
}
Copy after login

Solution: Two DIVs (IE6 and IE7 Support)

For additional cross-browser support, especially for IE6 and IE7, you can use two DIV elements instead of pseudo elements. This involves absolutely positioning the second DIV over the first and setting its z-index, opacity, and background-color to achieve the desired effect.

The above is the detailed content of How to Invert CSS Font Color Based on Background Color?. 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