gpt4 book ai didi

css - 是否可以在 CSS3 中混合透明色和纯色?

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:24 25 4
gpt4 key购买 nike

是否可以在 CSS3 中从纯色和透明色动态制作透明背景?例如:

<div class="red trans1">
CONTENT
</div>

使用 CSS

.red {
background: #FF0000;
}
.trans1
background: rgba(255,255,255,0.5);
}

在这种情况下,纯色将完全覆盖透明度。当然,我的意思是使用不同的属性(backgroundbackground-color 等)。

我有 10 种纯色,想为每种颜色创建 10 级透明度。如果单独制作每种颜色的透明色,需要100个CSS类;例如:

.red1 {
.background: rgba(255,0,0,0.1);
}
.red2 {
.background: rgba(255,0,0,0.2);
}
.red3 {
.background: rgba(255,0,0,0.3);
}
....
.blue1 {
.background: rgba(0,0,255,0.1);
}
.blue2 {
.background: rgba(0,0,255,0.2);
}
.blue3 {
.background: rgba(0,0,255,0.3);
}

我正在寻找一种混合纯色和透明背景的动态方式。

最佳答案

纯CSS

是的,您可以通过创造性地使用伪元素来分离颜色和透明度。例如,this fiddle demonstrates the following code (注意我已经根据 :after 伪元素安排了所有内容):

HTML

<div class="opBkg red op10">Red 10%</div>
<div class="opBkg red op50">Red 50%</div>
<div class="opBkg blue op80">Blue 80%</div>

相关 CSS

.opBkg {
position: relative;
}

.opBkg:after {
content: '';
position: absolute;
z-index: -1;
top: 0;
right: 0;
left: 0;
bottom: 0;
}

.red:after {
background-color: red;
}
.blue:after {
background-color: blue;
}
.op10:after {
opacity: .1;
}
.op50:after {
opacity: .5;
}
.op80:after {
opacity: .8;
}

您将有 10 条不透明度规则,无论您想要多少颜色,然后是总体 opBkg 类来进行设置。

关于css - 是否可以在 CSS3 中混合透明色和纯色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com