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>
Related
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>
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.
Here's my LESS statements:
#colorWhite: #FFFFFF;
#colorBlack : #000000;
#opacityNormalFill: 0.2;
#opacityNormalLabel: 0.75;
.colorWithAlpha(#color, #alpha)
{
#colorWithAlpha: rgba( red(#color), green(#color), blue(#color), #alpha );
}
if I write both background-color and color as this:
.button {
.colorWithAlpha(#colorBlack, #opacityNormalFill);
background-color: #colorWithAlpha;
.colorWithAlpha(#colorWhite, #opacityNormalLabel);
color: #colorWithAlpha;
}
The output will be:
.button {
background-color: rgba(0, 0, 0, 0.2);
color: rgba(0, 0, 0, 0.2);
}
I have to write it like this:
.button {
.colorWithAlpha(#colorBlack, #opacityNormalFill);
background-color: #colorWithAlpha;
}
.button {
.colorWithAlpha(#colorWhite, #opacityNormalLabel);
color: #colorWithAlpha;
}
It will output correctly:
.button {
background-color: rgba(0, 0, 0, 0.2);
}
.button {
color: rgba(255, 255, 255, 0.75);
}
How to resolve it?
Ok, your var #colorWithAlpha is limitted to your function .colorWithAlpha. If you try to use a global var, it will modifie all your code. You should pass the part to set this color in the function params like this :
#colorWhite: #FFFFFF;
#opacityNormalFill: 0.2;
#opacityNormalLabel: 0.75;
#colorBlack : #000000;
.colorWithAlpha(#color, #alpha, #property)
{
#{property} : rgba( red(#color), green(#color), blue(#color), #alpha );
}
And when you use it :
.button {
.colorWithAlpha(#colorBlack, #opacityNormalFill, background-color);
.colorWithAlpha(#colorWhite, #opacityNormalLabel, color);
}
less doesn't set global variables. You should be using the mixin as a nested style, not as a function.
Like:
.colorWithAlpha(#bgcolor, #color, #alpha)
{
background-color: rgba( red(#bgcolor), green(#bgcolor), blue(#bgcolor), #alpha );
color: rgba( red(#color), green(#color), blue(#color), #alpha );
}
Then:
.button {
.colorWithAlpha(#colorBlack, #colorWhite, #opacityNormalLabel);
}
Docs:
All variables defined in a mixin are visible and can be used in caller's scope (unless the caller defines its own variable with the same name).
Since your first .colorWithAlpha expansion does already define the #colorWithAlpha variable inside the .button, the second .colorWithAlpha call has no effect. (See #1892 for more details).
So you need either to isolate each expansion in its own scope:
.button {
.colorWithAlpha(#colorBlack, #opacityNormalFill);
background-color: #colorWithAlpha;
& { // <- begin new scope
.colorWithAlpha(#colorWhite, #opacityNormalLabel);
color: #colorWithAlpha;
}
}
Or use the solution suggested in #throrin19's answer.
---
And btw., to change color opacity use fade function, i.e. you don't need this mixin at all and your snippet can be simplified to:
#opacityNormalFill: 20%;
#opacityNormalLabel: 75%;
.button {
background-color: fade(#000, #opacityNormalFill);
color: fade(#fff, #opacityNormalLabel);
}
I used this in my button pushButton stylesheet
QPushButton#pushButton {
background-color: yellow;
}
QPushButton#pushButton:pressed {
background-color: rgb(224, 0, 0);
}
QPushButton#pushButton:hover {
background-color: rgb(224, 255, 0);
}
when I hover my mouse over it, it changes color, like I expect it to , But the hover color remains even when I press the button.
I tried changing the order, but its still the same problem .
little new in Qt.
You can combine states, for example:
QPushButton:hover:!pressed
{
border: 1px solid red;
}
QSS reference - states
Css, and Qt CSS, depends on the order of declarations. Later declarations with the same specificity will overwrite previous declarations. So, in order to have the pressed state take precedence, simply move it below the hover state.
QPushButton#pushButton {
background-color: yellow;
}
QPushButton#pushButton:hover {
background-color: rgb(224, 255, 0);
}
QPushButton#pushButton:pressed {
background-color: rgb(224, 0, 0);
}
This is the correct stylesheet as you want:
//base stylesheet
QPushButton
{
background-color: yellow;
}
//pressed button stylesheet
QPushButton:pressed
{
background-color: rgb(224, 0, 0);
}
//hover stylesheet
QPushButton:hover:!pressed
{
background-color: rgb(224, 255, 0);
}
You can set the image in QPushButton:
QPushButton#pushButton {
background-url(Images/image1.png);
}
QPushButton#pushButton:pressed {
background-url(Images/image2.png);
}
QPushButton#pushButton:hover {
background-url(Images/image3.png);
}
With the html code:
<div class="c1">Color 1</div>
<div class="c2">Color 2</div>
<div class="c1 c2">Mix of color 1 and 2</div>
and this css rules:
.c1 {
background-color: rgba(255,0,0,0.5)
}
.c2 {
background-color: rgba(0,255,0,0.5)
}
I would like the 3rd div be yellow. (it is green).
I know that it is possible to define another rule for the mix:
.c1.c2 {
background-color: rgba(255,255,0,0.5)
}
But imagine that I have lots of colors, or what it is worst: lots of classes with different rules that can be mixed.
Is there a way to do that in pure CSS without having to define the rules for all possible combinations?
You can do color operations with LESS
Goto http://less2css.org/
Copy this code into the input:
#red: rgba(255,0,0,0.5);
#green: rgba(0,255,0,0.5);
#yellow: #red + #green;
.test
{
background-color: #yellow;
}
which produces the css output:
.test {
background-color: #ffff00;
}
EDIT:
OK , after your comment and re-reading the question, I tthink this is what you want to do:
LESS:
#red: rgba(255,0,0,0.5);
#green: rgba(0,255,0,0.5);
.c1
{
background-color: #red;
}
.c2
{
background-color: #green;
}
.c1.c2
{
background-color: #red + #green;
}
which produces:
.c1 {
background-color: rgba(255, 0, 0, 0.5);
}
.c2 {
background-color: rgba(0, 255, 0, 0.5);
}
.c1.c2 {
background-color: #ffff00;
}
No, You have to use separate css for each color.
.c3 { background-color: Yellow;}
for third div.