Welcome to OStack Knowledge Sharing 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 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...