Is there a way to use CSS variables when specifying gradient colors with transparency, e.g.
:root {
--accent-color: #dfd0a5;
}
h1{
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(red(var(--accent-color)), green(var(--accent-color)), blue(var(--accent-color)), 1));
}
You can use variables, but you can't sample the individual red, green and blue components from a single hex value in CSS.
If you're simply looking to apply an alpha component to an existing RGB triplet, you can specify the entire triplet as a comma-separated list of decimal values instead of a hex value, and substitute it directly into the rgba() function as a single opaque token:
:root {
--accent-color: 223, 208, 165;
}
h1 {
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(var(--accent-color), 1));
}
If you want to specify and control individual R, G and B values with rgba(), you will need to specify a variable for each color component as a decimal value, and reference each variable within the rgba() function like so:
:root {
--accent-red: 223;
--accent-green: 208;
--accent-blue: 165;
}
h1 {
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(var(--accent-red), var(--accent-green), var(--accent-blue), 1));
}
#boltclock said it all, but you can save a bit of time if your project has a scss preprocessor.
You can do a little tweak to achieve what you want :
// Scss
#mixin defineColorRGB ($color, $red, $green, $blue) {
#{$color}: unquote("rgb(#{$red}, #{$green}, #{$blue})");
#{$color}-r: #{$red};
#{$color}-g: #{$green};
#{$color}-b: #{$blue};
}
Then in you css, you can do this:
::root {
#include defineColorRGB(--accentColor, red(#dfd0a5), green(#dfd0a5), blue(#dfd0a5));
}
You will end up with 4 different css variables, one for your color, and one for each color channel.
Then you can use it almost like you wrote it:
h1{
background: linear-gradient(
to right, rgba(255, 255, 255, 0),
rgba(var(--accent-color-r), var(--accent-color-g), var(--accent-color-b), 1)
);
}
I find it a very convenient way to initialize my css variables, and use it in most of my projects.
Related
I have an ion list that is contained in an ion content like so:
<IonContent class="sub-list-container" scrollY={true} style={{ "background": "red" }}>
<IonList class="sub-list">
<IonMenuToggle autoHide={false} key={p.title}>
{
p.children.map(el => el)
}
</IonMenuToggle>
</IonList>
</IonContent>
The styles for this are:
.sub-list-container {
--background: var(--primary-dark);
}
Inside the list is of course some ion items. I have customised their separator line to be a gradient of white to transparent like this:
border-image: linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(255, 255, 255, 1) 35%, rgba(255, 255, 255, 1) 50%, rgba(255, 255, 255, 1) 65%, rgba(255, 255, 255, 0) 100%) 1 1;
This is so the line will blend into any background colour that I put it on. This works fine for a list on a different page however on the list I showed above, it goes from white to black. The issue isn't the gradient but the colour of the background behind it. I've tried setting every list, every ion content in the menu to the colour I need the background to be but it still looks like this:
As you can see, even though background is grey (and even the ion content's background is grey too), the line still blends to black.
After doing some digging in chrome, I found that this:
.list-ios {
background: var(--ion-item-background, var(--ion-background-color, #fff));
}
is causing that. If I disable that style then I can see whatever background colour I set instead of the black like so:
Unfortunately I have not found a way to disable this style. The mode of the ion list can only be ios or md so it will always have to have one of these styles. Even setting the background colours to !important does not work.
Is there anyway I can override this list-ios style?
I need to apply color variable to liner-gradient, which also needs to have opacity. So in that case rgba(red, green, blue, opacity) is mandatory. Is there any way to apply variable color without hard coding rgba() in liner-gradient?
For instance :
let gradient= `linear-gradient(to bottom, rgba(255, 255, 255, 0) 15%, rgba(255, 255, 255, 0.9) 40%, rgba(255, 255, 255, 1) 20%)`
.arraow {
background: ${(p) => p.gradient}
}
The above code is only for one color and taht is #FFFFFF at the moment. But this needs to change depending on color selected for the gradient to use.
I need something like below:
const gradientColor = p.arrowColor;
let gradient= `linear-gradient(to bottom, var(gradientColor) 15%, var(gradientColor) 40%,
var(gradientColor) 20%)` <<<<<------ here I am missing the opacity since they are not rgba()
It doesn't seem a nice way to manually create all rgba() and keep it a file and access it with a conditional statement.
I am trying to generate a gradient using one single color and then varying the brightness or some other related variable.
Is it possible to do so ?
Use a back/white layer on the top of your single color as a gradient
html {
background:
linear-gradient(to right,rgba(255,255,255,0.5),rgba(0,0,0,0.5)),
red; /* here is your single color */
}
yes you can by use "transparent" as a color
background: linear-gradient(transparent, #9198e5);
You can use alpha channel for this purpose. Consider color "#428383":
div {
background-color: #428383; /* for browsers without gradient support */
background: linear-gradient(#42838344, #428383ff);
width: 100px;
height: 100px;
}
<div></div>
You can use RGBA
background: rgb(2,0,36);
background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(2,0,36,0.3) 100%);
0.3 will dim the color
If you want to add stop points at 0%, 30%, 100% with three different brightness
background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(2,0,36,0.4) 30%, rgba(2,0,36,1) 100%);
If you want to vary brightness or saturation of a single color, you may be better of using hsl() instead of hex or rgb values:
.gradient {
width: 200px;
height: 100px;
background: linear-gradient(hsl(193,82%,56%), hsl(193,82%,26%));
}
<div class="gradient"></div>
HSL stands for Hue, Saturation and lightness or luminance and is just another way of describing a color. This way you can easily alter saturation and brightness without actually altering the colors hue. Read more here: https://en.wikipedia.org/wiki/HSL_and_HSV
I am wondering if it is possible to add the opacity element to a sass variable? I am working on a project where I need to create different shades of a color and use them in custom typography file. My problem is when I create a color variable in rdga and implement it into my work the variable changes the code to a CSS opacity element which is written under a color element then this gets ran through the browser and throws and error. Is there a certain way to implement opacity in a variable so you don't get this problem?
Any help would be great, thanks
here are my variables:
$white-text-dh: rgba(255, 255, 255, 0.5);
$white-text-d: rgba(255, 255, 255, 0.12);
here is the typography ex:
.c-title{
font-size:20px;
color: $white-text-d;
font-family: Roboto-Light;
}
here is the html
<span class="c-title">hello</span>
this is what reads in the developer tools with an error going through the color. the color element can not read opacity
.c-title {
font-size: 20px;
color: #ffffff opacity 0.7%;
font-family: Roboto-Light;
}
I tested this and it works for me:
SCSS
$white: #fff;
$white-text-dh: rgba($white, .5);
.c-title {
color: $white-text-dh;
}
HTML
<h1 class="c-title">Test</h1>
Let a color variable used in sheet.less.
color is in HEX format #f57e20.
I want to use that color but add an alpha channel to it.
I want to end up with rgba(245, 126, 32, 0.5)
Does Bootstrap or less have anything to do this?
There are a some built in functions in less.js and mixins from Bootstrap that you could use:
This is a less.js function:
// using a variable
#color: #f57e20;
.test {
background: fade(#color, 50%); // return #color with 50% transparency
}
// or without the color variable
.test2 {
background: fade(#f57e20, 50%); // return #color with 50% transparency
}
These both result in:
.test {
background: rgba(245, 126, 32, 0.5);
}
.test2 {
background: rgba(245, 126, 32, 0.5);
}
Or use a Bootstrap mixin:
.test3 {
#translucent > .background(#f57e20, 0.5); // use HSLA mixin
}
Which results in:
.test3 {
background-color: rgba(244, 123, 31, 0.5);
}
I'll include the (fixed) code for the translucent mixins here for archiving purposes, since (last I checked) it is no longer included as of Bootstrap 3.0.0:
// Add an alphatransparency value to any background or border color (via Elyse Holladay)
#translucent {
.background(#color: #white, #alpha: 1) {
background-color: fade(#color, #alpha);
}
.border(#color: #white, #alpha: 1) {
border-color: fade(#color, #alpha);
.background-clip(padding-box);
}
}
You may want to try:
background: rgba(red(#color), green(#color), blue(#color), #alpha);
I had to do something similar when writing a quick mixin which used box-shadow.