RGBA — Alpha channel as a separate class - css

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.

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>
And for better support you can consider pseudo element to create the background layer and adjust opacity:
div {
position: relative;
z-index: 0;
}
div::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: -1;
}
.bgr-red::before {
background-color: rgb(255, 0, 0);
}
.bgr-green::before {
background-color: rgb(0, 0, 255);
}
.bgr-blue::before {
background-color: rgb(0, 128, 0);
}
.alpha-90::before {
opacity: 0.9;
}
.alpha-70::before {
opacity: 0.7;
}
.alpha-10::before {
opacity: 0.1;
}
<div class="bgr-red">...</div>
<div class="bgr-red alpha-70">...</div>
<div class="bgr-red alpha-10">...</div>

Related

How to do something based on CSS variable content?

Let's say I have a CSS variable:
div {
--test: "hey"
}
And I would like to check what is inside this variable and do something based on this.
For example:
if var(--test) == "Hi":
margin-left: 1rem;
else:
padding-bottom: 1rem;
natively isn't possible, but with a css compiler you can!
I suggest you use SASS/SCSS for this:
https://sass-lang.com/ (is a CSS compiler, that let you write CSS in a comfortable way, then compile it (translating it) to a CSS native)
for using IF/ELSE see these docs https://sass-lang.com/documentation/at-rules/control/if
I would like to check what is inside this variable and do something based on this.
Yes, you can check the value of a CSS Custom Property natively using:
window.getComputedStyle(myDiv).getPropertyValue('--test')
Once you know the value of --test, you can either update one (or several) properties:
myDiv.style.setProperty('padding-bottom', '1rem');
or you can add a class which updates one property (or any number of properties) of myDiv:
myDiv.classList.add('increaseBottomPadding');
Working Example:
const divs = document.querySelectorAll('div');
divs.forEach((div) => {
let testValue = window.getComputedStyle(div).getPropertyValue('--test');
switch (testValue) {
case ('rainbow1') : div.classList.add('background1'); break;
case ('rainbow2') : div.classList.add('background2'); break;
case ('rainbow3') : div.classList.add('background3'); break;
case ('rainbow4') : div.classList.add('background4'); break;
}
});
div {
display: inline-block;
float: left;
width: 120px;
height: 120px;
margin-right: 12px;
background-color: rgb(255, 0, 0);
border: 8px solid rgba(255, 255, 255, 0.5);
border-radius: 50%;
box-shadow: 0 0 12px rgba(0, 0, 0, 0.5);
}
.div1 {
--test: rainbow1;
}
.div2 {
--test: rainbow2;
}
.div3 {
--test: rainbow3;
}
.div4 {
--test: rainbow4;
}
.background1 {
background-color: rgb(255, 0, 0);
}
.background2 {
background-color: rgb(255, 127, 0);
}
.background3 {
background-color: rgb(255, 255, 0);
}
.background4 {
background-color: rgb(0, 127, 0);
}
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>

CSS blending mode to replicate additive blending

I'm trying to create an RGB additive color model on my website.
I'm using the following code:
#colorBox1 {
background-color: rgb(255, 0, 0);
}
#colorBox2 {
background-color: rgb(0, 255, 0);
mix-blend-mode: screen;
}
#colorBox3 {
background-color: rgb(0, 0, 255);
mix-blend-mode: screen;
}
#colorBox4 {
background-color: rgb(255, 255, 255);
border: 1px solid black;
}
<div id="color">
<div id="colorBox1" class="colorBox"></div>
<div id="colorBox2" class="colorBox"></div>
<div id="colorBox3" class="colorBox"></div>
<div id="colorBox4" class="colorBox"></div>
</div>
* I've skipped some of the positioning rules to make the code as clear as possible
So I was expecting the 3 colors to blend into white color of rgb(255, 255, 255). But when I preview the page the color is a little bit off-white. It is actually rgb(248, 255, 253). Although rgb(255, 255, 255) (colorBox4) does display as pure white of (255, 255, 255) - all via a color picker.
Is this it - css colors are different?
Is it just the monitor?
Or is there a CSS blending method or a chain of CSS blending methods where I can achieve "true" color additive mode?
I am not seeing the results you are. In this test the colors are correctly blended. How are you determining the color of the blended region?
.colorBox{
position:absolute;
width: 100px;
height: 100px;
border-radius: 50%;
}
#colorBox1 {
background-color: rgb(255,0,0);
}
#colorBox2 {
background-color: rgb(0, 255, 0);
mix-blend-mode: screen;
left: 70px;
}
#colorBox3 {
background-color: rgb(0, 0, 255);
mix-blend-mode: screen;
top: 70px;
left:40px;
}
<div id="color">
<div id = "colorBox1" class="colorBox"></div>
<div id = "colorBox2" class="colorBox"></div>
<div id = "colorBox3" class="colorBox"></div>
</div>

Change z-index with counter-increment

I try to make an image slider which will show a picture when a mouse hovers over a dot. I tried too switch between images by using z-index but nothing moved.
.slider {
counter-reset: index 1000;
}
.slider input[name='slide_switch']:hover+label+img {
counter-increment: index;
z-index: counter(index);
}
The way you were trying to use counter wasn't going to work even if you used JavaScript/jQuery. The counter properties are used to number elements like an ordered list it has nothing to do with z-index. The best you can do is to rely on CSS animation which you can see in the following snippet. The key properties were:
transition: all 3s a long duration is needed to view z-index animated.
color: rgba(R, G, B, A) A is an opacity value that can change from totally visible to invisible, plus the levels of transparency between.
position: absolute/relative is not only required for z-index but also helpful for vertical and horizontal dimensions for elements as well.
calc() a function that will apply a simple equation for CSS properties. One of it's best features is that will work with a combination of absolute (e.g. px, pt, etc.) and/or relative (e.g. em, %, etc.) values.
When hovering over a circle, keep the cursor there for 3 sec. Animating z-index is a slow process because at faster speeds the progressive fading won't be noticeable.
Snippet
html {
font: 400 12px/1.2 'Consolas';
}
.slider {
position: relative;
width: 250px;
height: 250px;
}
output {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: all 3s;
display: block;
}
output b {
position: absolute;
font-size: 5rem;
top: calc(125px - 2.5rem);
text-align: center;
display: block;
width: 100%;
}
label {
z-index: 100;
position: relative;
height: 25px;
width: 25px;
padding: 5px;
cursor: pointer;
display: inline-block;
}
label b {
z-index: 100;
position: relative;
height: 15px;
width: 15px;
border: 1px solid #fff;
border-radius: 12px;
cursor: pointer;
margin: 5px;
display: inline-block;
padding: 1px 1px 0;
text-align: center;
color: #fff;
}
#A {
z-index: 10;
background: rgba(190, 0, 0, .5);
}
#B {
z-index: 20;
background: rgba(0, 0, 190, .5);
}
#C {
z-index: 30;
background: rgba(255, 50, 0, .5);
}
#D {
z-index: 40;
background: rgba(50, 200, 50, .5);
}
#E {
z-index: 50;
background: rgba(210, 100, 55, .5);
}
#F {
z-index: 60;
background: rbga(255, 200, 0, .5);
}
#a:hover~#A {
z-index: 70;
transition: all 3s;
background: rgba(190, 0, 0, 1);
}
#b:hover~#B {
z-index: 70;
transition: all 3s;
background: rgba(0, 0, 190, 1);
}
#c:hover~#C {
z-index: 70;
transition: all 3s;
background: rgba(255, 50, 0, 1);
}
#d:hover~#D {
z-index: 70;
transition: all 3s;
background: rgba(50, 200, 50, 1);
}
#e:hover~#E {
z-index: 70;
transition: all 3s;
background: rgba(210, 100, 55, 1);
}
#f:hover~#F {
z-index: 70;
transition: all 3s;
background: rgba(255, 200, 0, 1);
}
label:hover {
background: rgba(255, 255, 255, .1);
color: #000;
}
.top {
z-index: 75;
background: rgba(255, 255, 255, 1);
position: absolute;
width: 250px;
height: 205px;
transition: all 3s
}
label:hover~.top {
z-index: 0;
background: rgba(255, 255, 255, .1);
transition: all 3s
}
hr {
position: relative;
z-index: 101;
}
<fieldset class='slider'>
<label id="a" for="A"><b>A</b></label>
<label id="b" for="B"><b>B</b></label>
<label id="c" for="C"><b>C</b></label>
<label id="d" for="D"><b>D</b></label>
<label id="e" for="E"><b>E</b></label>
<label id="f" for="F"><b>F</b></label>
<hr/>
<output id="A"><b>A</b></output>
<output id="B"><b>B</b></output>
<output id="C"><b>C</b></output>
<output id="D"><b>D</b></output>
<output id="E"><b>E</b></output>
<output id="F"><b>F</b></output>
<div class='top'> </div>
</fieldset>
This is a neat idea, but runs into one or two problems:
The CSS counter() function returns a <string> but the z-index property is looking for an <integer>.
Browsers don't really support it anyway.
The MDN counter() docs note that:
The counter() function can be used with any CSS property, but support for properties other than content is experimental, and support for the type-or-unit parameter is sparse.
It's not completely clear what they mean by "the type-or-unit parameter" since none is discussed on that page. The attr() function does allow returning alternate types (e.g. an integer) via a <type-or-unit> parameter. I wonder if someone was toying with adding the same parameter to counter() at some point; if so, this appears to have been abandoned long ago.

Editing R studio theme in cache.css theme file (ACE editor?)

In RStudio, I have found the cache.css files for the different themes in Rstudio. They are text files with code like this:
.ace_editor { border: 2px solid rgb(159, 159, 159); } .ace_editor.ace_focus { border: 2px solid #327fbd; } .ace_gutter { background: #232323; color: #F8F8F8; } .ace_print_margin { width: 1px; background: #232323; } .ace_scroller { background-color: #141414;
} .ace_text-layer { color: #F8F8F8; } .ace_cursor { border-left: 2px solid #A7A7A7; } .ace_cursor.ace_overwrite { border-left: 0px; border-bottom: 1px solid #A7A7A7; } .ace_marker-layer .ace_selection { background: rgba(221, 240, 255, 0.20); } .multiselect
.ace_selection.start { box-shadow: 0 0 3px 0px #141414; border-radius: 2px; } .ace_marker-layer .ace_step { background: rgb(102, 82, 0); } .ace_marker-layer .ace_bracket { margin: -1px 0 0 -1px; border: 1px solid rgba(255, 255, 255, 0.25); } .ace_marker-layer
.ace_active_line { background: rgba(255, 255, 255, 0.031); } .ace_gutter_active_line { background-color: rgba(255, 255, 255, 0.031); } .ace_marker-layer .ace_selected_word { border: 1px solid rgba(221, 240, 255, 0.20); } .ace_invisible { color: rgba(255,
255, 255, 0.25); } .ace_keyword, .ace_meta { color:#CDA869; } .ace_constant, .ace_constant.ace_other { color:#CF6A4C; } .ace_constant.ace_character, { color:#CF6A4C; } .ace_constant.ace_character.ace_escape, { color:#CF6A4C; } .ace_invalid.ace_illegal
{ color:#F8F8F8; background-color:rgba(86, 45, 86, 0.75); } .ace_invalid.ace_deprecated { text-decoration:underline; font-style:italic; color:#D2A8A1; } .ace_support { color:#9B859D; } .ace_support.ace_constant { color:#CF6A4C; } .ace_fold { background-color:
#AC885B; border-color: #F8F8F8; } .ace_support.ace_function { color:#DAD085; } .ace_storage { color:#F9EE98; } .ace_variable { color:#AC885B; } .ace_string { color:#8F9D6A; } .ace_string.ace_regexp { color:#E9C062; } .ace_comment { fontSize:4pt;font-style:italic;
color:#996633; } .ace_variable { color:#7587A6; } .ace_xml_pe { color:#494949; } .ace_meta.ace_tag { color:#AC885B; } .ace_entity.ace_name.ace_function { color:#AC885B; } .ace_markup.ace_underline { text-decoration:underline; } .ace_markup.ace_heading
{ color:#CF6A4C; } .ace_markup.ace_list { color:#F9EE98; } .ace_indent-guide { background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAACCAYAAACZgbYnAAAAEklEQVQImWMQERH5zzBz5sz/AA5EBAYqeZXWAAAAAElFTkSuQmCC) right repeat-y; } .nocolor.ace_editor
.ace_line span {color:#CDA869 !important;} .ace_bracket {margin: 0 !important; border: 0 !important; background-color: rgba(255, 255, 255, 0.25);} .ace_marker-layer .ace_foreign_line {position: absolute; z-index: -1; background-color: rgb(65, 65, 65);}
.ace_marker-layer .ace_find_line {position: absolute; z-index: -1; background-color: rgb(134, 134, 134);} .ace_marker-layer .ace_active_debug_line {position: absolute; z-index: -1; background-color: rgb(137, 121, 38);} .ace_console_error { background-color:
rgb(65, 65, 65); }
I am trying to edit in these, and I could use some help. So far, I have only been able to change the colour of "comments" by finding and changing the colour indicated after the "ace.comment" command. But I would like to do something similar with things like the background colour, text colour, and colour of commands in R, colour of curly brackets, etc, etc.
Is this possible? If so, what things do I need to edit in order to accomplish this?
Yes it is indeed possible. (Remember to restart Rstudio after each change)
I have no experience about ACE stuff, but it's not so hard to get the meaning of the different variables.. e.g
.ace_constant.ace_numeric {
color: #4ef971;
}
...
.ace_string {
color: #6A8F9D;
}
...
.ace_keyword {
color: #85cfda;
}
are, respectively, colors of numeric objects, strings and keywords (built in-functions and console)
I found that the default web inspector with RStudio made it difficult to make edits. I have been trying for days to get folded comments to have different font attributes based on sections.
I found it was much easier using ATOM (see screenshot below). Once I found out what each variable changed I created comments in the css file to make it easier for me next time I edit. You can see in ATOM the color is shown, making it easier to identify what the variable is defining.
I am not sure if this is optimal but it is as far as I have got. I don't know why people don't comment on variable definitions, especially since some of them are not intuitive.

CSS named rgba colors

I would like to define colors with opacity
I know that it is possible to do this via rgba colors, but I like the ease of named colors. e.g.:
background-color: red;
Is there a way to combine the two?
The only way would be to apply the color and the opacity separately like this:
someElement {
background: blue;
opacity: .5;
}
But, this won't be possible to do selectively since opacity applies to the entire element, not any one aspect of the element. Here, you'll see that the background color and the font color are both affected.
div { background: yellow;
color:green;
opacity: .5;
}
<div>This is my div</div>
No...this is not possible in raw CSS.
CSS preprocessors however can do this
SASS
div {
width: 100px;
height: 100px;
background-color: rgba(red,0.5);
}
compiles to
CSS
div {
width: 100px;
height: 100px;
background-color: rgba(255, 0, 0, 0.5);
}
SASSMeister Demo
You can't use "red" in rgba(red,0.5) with standard CSS, but you can do
background: red;
opacity: 0.5;
The down side is that opacity will affect child elements, which rgba(255,0,0,0.5) will not.
By using pseudo element you can create a similar effect.
div {
position: relative;
height: 30px;
}
div:first-child {
background: rgba(255,0,0,0.5);
margin-bottom: 10px;
}
div ~ div:before {
content: ' ';
position: absolute;
left: 0; top: 0;
right: 0; bottom: 0;
background: red;
opacity: 0.5;
z-index: -1;
}
<div>Hello</div>
<div>Hello</div>
I'm afraid that you can't. There are no named alpha channel colors in the css3 specification.
You can read more about it in
https://drafts.csswg.org/css-color-3/#colorunits
If what you want is to have a nemonic to apply to backgrounds, may be the best is to create a class for that:
.red-50 {
background-color: rgba(255, 0, 0, 0.5);
}
.red-75 {
background-color: rgba(255, 0, 0, 0.75);
}
And apply to your html elements
<div class="red-50 someOtherClass"></div>
The CSS3 rgba() and hsla() functions are the only way to directly specify a color with alpha. However, if use of less or SASS is allowed, colors can be assigned to variables and used similarly to the builtin named colors. A less example:
#red50: rgba(255, 0, 0, 0.5);
.something {
background-color: #red50;
}

Resources