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
1.2k views
in Technique[技术] by (71.8m points)

css - RGBA — Alpha channel as a separate class

Let's say we have a list of classes storing background colors.

.bgr-red //background-color: rgb(255, 0, 0);
.bgr-green //background-color: rgb(0, 0 , 255);
.bgr-blue //background-color: rgb(0, 128, 0);

And we have a div using one of these classes.

<div class="bgr-red">...</div>

Is there any way that I can create a new set of classes which contain alpha channels? Something like this (I tried this method, it didn't work):

.alpha-90 //background-color: rgba(inherit, inherit, inherit, .9);
.alpha-80 //background-color: rgba(inherit, inherit, inherit, .8);
.alpha-70 //background-color: rgba(inherit, inherit, inherit, .7);

The end objective being to be able to place background color opacity into a div separate from the rest of the background color value? Creative a div something like this:

<div class="bgr-red alpha-80">...</div>

Thank you.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Use CSS variables:

.bgr-red {
  background-color: rgba(255, 0, 0, var(--a, 1));
}

.bgr-green {
  background-color: rgba(0, 0, 255, var(--a, 1));
}

.bgr-blue {
  background-color: rgba(0, 128, 0, var(--a, 1));
}

.alpha-90 {
  --a: 0.9;
}

.alpha-70 {
  --a: 0.7;
}

.alpha-10 {
  --a: 0.1;
}
<div class="bgr-red">...</div>

<div class="bgr-red alpha-70">...</div>

<div class="bgr-red alpha-10">...</div>

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

...