CSS variables and opacity - css

A quick question to which the answer is probably "NO", but I'm new to CSS variables so I'm not sure.
If I want to define color and later be able to add alpha channel to it, is my only option with CSS variables would be to define it as 3 numbers for RGB channels:
--color: 12 12 12
And later use it ALWAYS with rgb or rgba?
color: rgb(var(--color));
background: rgba(var(--color), .5);
There is really no way to define an actual color and later add alpha to it using only CSS variables? Probably my best bet would be to define 2 vars:
--color-rgb and --color: rgb(var(--color-rgb))

You are almost good, you simply need to pay attention to the syntax:
:root {
--c:255,0 ,0;
--o:0.5;
}
html {
background:rgba(var(--c),var(--o));
}
body {
background:rgb(var(--c));
height:100px;
}
.box {
--c:12,12,12;
--o:0.7;
background:rgba(var(--c),var(--o));
color:#fff;
}
<div class="box">
some content
</div>
You can also define each channel alone:
:root {
--r:255;
--g:0;
--b:0;
--c:var(--r),var(--g) ,var(--b);
--o:0.5;
}
html {
background:rgba(var(--c),var(--o));
}
body {
background:rgb(var(--c));
height:100px;
}
.box {
--c:12,12,12;
--o:0.7;
background:rgba(var(--c),var(--o));
color:#fff;
}
<div class="box">
some content
</div>

Related

How to select specific class prefix while excluding other prefix?

I need to select the body element when it has a class beginning with post-type- but not select it when there's also a class beginning with taxonomy-. Does anyone know how to get this to work?
body[class^="post-type-"],
body[class*=" post-type-"] {
&:not([class^="taxonomy-"]),
&:not([class*=" taxonomy-"]) {
.widefat {
.check-column {
display: none;
}
}
}
}
EDIT: 0stone0's answer below helped me realize the CSS it was outputting was completely wrong, so this new approach is working well:
body[class^="post-type-"]:not([class^="taxonomy-"]):not([class*=" taxonomy-"]),
body[class*=" post-type-"]:not([class^="taxonomy-"]):not([class*=" taxonomy-"]) {
.widefat {
.check-column {
display: none;
}
}
}
div[class*='post-type-']:not([class*="taxonomy-"])
This pure CSS should target the desired element
Select classes that contain post-type-, but not() containing taxonomy-
div[class*='post-type-']:not([class*="taxonomy-"]) {
border: 1px solid red;
}
<div class='post-type-something'>post-type-something</div>
<div class='post-type-something taxonomy-foobar'>post-type-something taxonomy-foobar</div>
<div class='taxonomy-foobar post-type-something'>taxonomy-foobar post-type-something</div>
Note: Demo uses <div> instead of <body> and applies a border when targeted

Advanced SCSS structure: E.g. dynamic variables depending on the existence of a specific parent/outer class

I'm trying to find out whether the following logic, that would allow for a more modular approach, can be implemented or not:
Challenge:
I got three major categories in my app: running, swimming, biking, each with their own theme/color, e.g.
scss:
$category-colors: (
'running': red;
'swimming': blue;
'biking': green;
)
Throughout the app, specific components and nested components should adapt e.g. their background-color to the color of the context, meaning to the color of one of the categories. The following is one such component:
html:
<div class="running">
<div class="form">
<div class="form__header">
<h3>New</h3>
</div>
<div class="form__content">
<p>Some text</p>
</div>
</div>
</div>
scss:
.form {
&__header {
height: 100px;
// 'running' is not available here but it illustrates what I'm trying to achieve
background: map-get($category-colors, 'running');
}
&__content {
height: 50vh;
}
}
The background color should be dynamically set depending on the presence of the parent/grandparent/n-parent running. Is there any way to get 'category' dynamically?
I could to add a suffix for the class that should adopt that theme/color like form__header_running but I couldn't find a way to extract "running" as a variable to then map the color.
I tried something like the following, but the variables somehow always ends up to be the last one in that chain, meaning 'biking', even if biking is not present on the parent.
.form {
$bgColor: black !default;
.running & {
$bgColor: map-get($category-colors, 'running')
}
.swimming & {
$bgColor: map-get($category-colors, 'swimming')
}
.biking & {
$bgColor: map-get($category-colors, 'biking')
}
&__header {
height: 100px;
background: $bgColor;
}
&__content {
height: 50vh;
}
// image much more children/sub children, so the simple use of $bgColor would be much more efficient
}
Any idea/concepts on how to achieve what I want?
Any inputs on a styling structure/architecture you would choose for a larger app with similar demands?
Looking forward to a discussion!

How can I overrule a LESS variable within a mixin?

I'm using LESS in one of my projects right now and I need to create some colour-schemes for different users. I'm building a portal and based on what kind of user logs in, the colour-scheme needs to change.
So in my mixins.less (which I can't modify) file I have:
#color-button-bg: #333;
#color-button-txt: #fff;
#color-button-fs: 1.5rem;
#color-button-fw: 500;
#color-button-hover-pct: 10%;
.base-btn-default(#type: button) {
background: ~"#{color-button-bg}";
border: 1px solid ~"#{color-button-bg}";
color: ~"#{color-button-txt}";
font-size: ~"#{color-button-fs}";
font-weight: ~"#{color-button-fw}";
&:hover, &:focus {
#color-btn-hover: ~"color-button-bg";
#color-btn-hover-pct: ~"color-button-hover-pct";
background: darken(##color-btn-hover,##color-btn-hover-pct);
border-color: darken(##color-btn-hover,##color-btn-hover-pct);
color: ~"#{color-button-txt}";
}
}
And in a separate file with client-specific mixins I tried the following:
/* Override default color with theme-color */
.theme-styling() {
#color-button-bg: #main-theme-color;
}
Then finally I wanted to add a class to my <body> tag and style the colour-scheme based on that classname:
body.theme-a {
#main-theme-color: teal;
.theme-styling();
}
However, this doesn't seem to work. I think it has something to do with scoping / Lazy evaluation, but I'm not that experienced in LESS yet, to see where my error is.
I created a Codepen for it, without the separate files and in a bit of a simplified form:
https://codepen.io/jewwy0211/pen/JVNZPv
I've just played around with your codepen a bit. When you define the variable in your mixin, it does work, the problem is that you then don't use that variable in the mixin or in the class that contains the mixin.
So, if you've got 2 buttons like this:
<div class="wrapper one">
<button>Theme 1</button>
</div>
<div class="wrapper two">
<button>Theme 2</button>
</div>
You can call their theme-specific variable mixins in their respective classes to get the vars set, but you then must use the vars within the same class:
.wrapper.one {
.theme-styling-1();
background-color: #color-button-bg;
}
.wrapper.two {
.theme-styling-2();
background-color: #color-button-bg;
}
/* Theme Vars */
.theme-styling-1() {
#color-button-bg: teal;
}
.theme-styling-2() {
#color-button-bg: red;
}
EDIT: Here's a codepen: https://codepen.io/mmshr/pen/OGZEPy

What is the LESS equivalent of declaring var in CSS?

I have a scenario to change the branding color and few other styling variables on the portal at run time based on some conditions. I'm able to achieve that
using CSS (look at the below sample snippet).
But since all our CSS are generated by converting the LESS files at compile time, not sure on how to write a similar logic in LESS so that it generates the CSS like below?
:root {
--brand-color: yellow;
}
h1 {
color: var(--brand-color);
}
<h1 id="demo">Hello</h1>
<button onclick="myFunction()">Click me</button>
function myFunction() {
document.documentElement.style.setProperty('--brand-color', 'red');
}
#yellow-color: yellow;
#red-color: red;
h1{
color: #yellow-color;
&.red-color{
color: #red-color;
}
}
Add class instead
function myFunction(){
$("myelement").addClass("red-color");
}

nesting css class for sprite image

I'm just bit confused on css Id/class nesting.
sample code below:
1) #sprit-img {
display:inline;
border:1px solid #FFF;
text-decoration:none;
display:block;
float:left;
width:50px;
height:50px;
background-image:url(ig-sprite.png);
background-repeat:no-repeat;
margin:5px;
}
2) #sprit-img a.brew{
background-position:2px 0px;}
3) #sprit-img a.scc{
background-position:-295px 2px;}
in page i used like
4) <div id="sprit-img><a class="brew"></a>...</div> `
now i want to use it like
5) <div class="sprit-img"><a class="brew"></a> <span class="scc"></span></div>`
Questions
is it necessary to give anchor or any element tag in code line 2 and 3?
what would be optimal way to get line 5(if Q1 is true, to have in css class i removed # and place . but not working in my page)? is this correct
--
6) .sprit-img{.....same code..}
.brew{...same position..}
.scc{..same postion...}
and use it like in line 5 or
this is correct
7) .sprit-img{.....same code..}
.sprit-img .brew{...same position..} `
Thanks.
edit: I tried some mix put background-image from sprit-img to brew and scc and found that if i put style as in 6 the html part should be like
<div class="anything"><span class="sprit-img brew"></span></div>
and if i put style like in 7 html part should be like
<div class="sprit-img"><span class="sprit-img brew"></span></div>
but could not make it like 5 any idea ...
Q.1: No.
Q.2:
You need a way to target both types of things inside your div at once in order to apply the same bg image, as well as a way to differentiate them. There are numerous solutions.
As long as you're sure that anything nested inside sprit-img should take the background, you could do this:
<div class="sprit-img">
<a class="brew"></a> <span class="scc"></span>
</div>
#sprit-img * { background-image:url(ig-sprite.png) };
#sprit-img .bew { background-position:2px 0px }
#sprit-img .scc { background-position:-295px 2px; }
(note: * is the universal selector)
...though that could get you in trouble if you need any markup inside of those elements (everything would take the background image, and it would be a funky jumble)
So, if you are sure all child elements (nested only 1 level down) should take the background, but nothing inside of those elements should, then you can use the child selector ( > ) like this:
<div class="sprit-img">
<a class="brew"><span> some text></span>some other text</a>
<span class="scc">more text <strong>something important</strong></span><
</div>
#sprit-img > * { background-image:url(ig-sprite.png) };
#sprit-img .bew { background-position:2px 0px }
#sprit-img .scc { background-position:-295px 2px; }
if you want to avoid the universal selector (rendering could be slow on older machines or with earlier browser versions), you could alternatively use (with line 5 markup):
#sprit-img > a, #sprit-img > span { background-image:url(ig-sprite.png) };
#sprit-img .bew { background-position:2px 0px }
#sprit-img .scc { background-position:-295px 2px; }
...which would then only apply to anchors and spans inside of an element with id="sprit-img"
Or you coulld avoid tag-names altogether (if you are super render-speed conscious) (with line 5 markup)
#sprit-img .bew, #sprit-img .scc { background-image:url(ig-sprite.png) };
#sprit-img .bew { background-position:2px 0px }
#sprit-img .scc { background-position:-295px 2px; }
...which illustrates why the answer to your first question is "no"
# is an ID selector, . is a class selector. So you could change to:
.sprit-img {...}
.sprit-img .brew {...}
.sprit-img .scc {...}
and
<div class="sprit-img"><a class="brew"></a> <span class="scc"></span></div>
However, the real problem is your trying to use non-cascading properties in .sprit-img on the child elements. The first selector should be changed to .sprit-img .brew, .sprit-img .scc

Resources