Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

html - Invert colored text related background

I want to invert text color based on the background of the text, like in this image: image.

So I've tried the following code, but it didn't work:

#warp,
#text,
#tri {
  position: absolute;
  top: 0;
  left: 0;
  width: 150px;
  height: 150px;
  z-index: 1;
}

#warp {
  background-color: orange;
}

#text {
  text-align: center;
  z-index: 3;
}

#text h1 {
  margin: 0;
  line-height: 150px;
  mix-blend-mode: difference;
}

#tri {
  background-color: black;
  clip-path: polygon(0 0, 0 100%, 99.8% 150px);
  -webkit-clip-path: polygon(0 0, 0 100%, 99.8% 150px);
  z-index: 2;
}
<div id="warp">
  <div id="text">
    <h1>TEXT</h1>
  </div>
  <div id="tri"></div>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can avoid clip-path by using linear-gradient as background and the mix-blend-mode will work perfectly:

#text {
  width: 150px;
  height: 150px;
  z-index: 1;
}

#text {
  text-align: center;
  background: linear-gradient(to top right, black 50%, orange 51%);
}

#text h1 {
  margin: 0;
  line-height: 150px;
  mix-blend-mode: difference;
  color: #fff;
}
<div id="text">
  <h1>TEXT</h1>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...