文本颜色适配背景色
使用mix-blend-mode实现文本颜色随着背景颜色自动变化
关键知识点
关键代码
mix-blend-mode: difference;
完整代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta
http-equiv="X-UA-Compatible"
content="IE=edge" />
<meta
name="viewport"
content="width=device-width,initial-scale=1.0" />
<title>文本颜色适配背景色</title>
<meta
name="keywords"
content="demo,js,css,animate,技术方" />
<style>
body {
background: #eee;
margin: 0;
padding: 8px;
}
.outbox {
width: 400px;
max-width: 90vw;
margin: 0 auto;
}
.container {
width: 100%;
padding-bottom: 100%;
position: relative;
}
@property --direct {
syntax: '<angle>';
initial-value: 0deg;
inherits: false;
}
.card {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
--border-width: 1rem;
border-radius: 0.25rem;
padding: var(--border-width);
--direct: 0deg;
background: linear-gradient(var(--direct), black 50%, white 50%);
animation: rotate 2s linear infinite;
}
@keyframes rotate {
to {
--direct: 360deg;
}
}
.inner {
color: #fff;
mix-blend-mode: difference;
font-size: 32px;
text-align: center;
}
</style>
</head>
<body>
<div class="outbox">
<div class="container">
<div class="card">
<div class="inner">文本颜色适配背景色</div>
</div>
</div>
</div>
</body>
</html>