在我的网站中,我需要显示一个背景为白色的按钮,文本将获得下面背景的颜色,该颜色可以是纯色或背景图像。
由于背景和按钮位置是动态的,我不能使用的解决方案,其中写到这里。
请参阅下面的示例。
我正在寻找css唯一的解决方案。
感谢您的帮助。
您可以通过在文本前面添加按钮的背景,使用伪元素,然后mix-blend-mode在按钮(screen)和伪元素(color-burn)上创建抠图效果,来获得此效果。
注意:这仅在按钮的背景为白色时才有效。
注意: mix-blend-mode IE和Edge不支持。
.button {
position: relative;
padding: 0.5em 1em;
font-size: 1.2em;
border: none;
background: none;
outline: none;
color: black;
mix-blend-mode: screen;
}
.button::before {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: white;
border-radius: 0.3em;
content: '';
mix-blend-mode: color-burn;
}
.bg {
padding: 2em;
}
.bg1 {
background: purple;
}
.bg2 {
background: linear-gradient(to bottom, gold 50%, silver 50%);
}Run Code Online (Sandbox Code Playgroud)
<div class="bg bg1">
<button class="button">Text</button>
</div>
<div class="bg bg2">
<button class="button">Text</button>
</div>Run Code Online (Sandbox Code Playgroud)