Questions tagged [css-variables]

Custom properties (or CSS variables) allow to define stylesheet-wide values identified by a token and usable in all CSS declarations.

Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document.

developer.mozilla

They are set using custom property notation (e.g., --main-color: black;) and are accessed using the var() function (e.g., color: var(--main-color);).

Example

/* Declaring a variable */
:root {
  --main-bg-color: brown;
}

/* Using the variable */
element {
  background-color: var(--main-bg-color);
}
625 questions
12
votes
2 answers

Angular use CSS variable in global style.css file

How to use CSS variable in global CSS file Just check style.css file in stackblitz https://stackblitz.com/edit/angular-themeing-y3jwrk?file=src/styles.css
ksh
  • 639
  • 1
  • 7
  • 23
12
votes
6 answers

Conditional styles with CSS custom properties

I enjoy using CSS custom properties, but there's this thing that I often find that I wish I could do. I wish to be able to apply some styles conditionally based on the value of a CSS custom property. Here's some pseudo-code: .something { border:…
timetowonder
  • 5,121
  • 5
  • 34
  • 48
11
votes
2 answers

CSS variable not working within SCSS when I use var and rgba

I have searched around for this but none seems to work. I am working on Angular and have my scss variables on the root file, styles.scss in the :root pseudo-selector. The following works in my component scss; :root…
Novak254
  • 439
  • 6
  • 14
11
votes
2 answers

Listen to CSS variable change?

Is it possible to use a callback to listen to CSS variable changes? something like this: documentElement.addListener('css-var-main-background-color', () => { console.log('Value changed'); });
Hammerhead
  • 1,044
  • 8
  • 19
11
votes
2 answers

How to Use calc() to switch between color values?

Is it possible to use calc() function in CSS to manipulate hexadecimal color value? In following CSS snippet I would like to use --enable variable to switch between two hexadecimal color values for background-color property of the MyBtnStyle:…
Miroslav Radojević
  • 487
  • 1
  • 5
  • 20
11
votes
2 answers

Can a recursive variable be expressed in css?

For the html:
...
Are there any ways to create a recursive variable that uses its parent's value: body > div { --x: 1; } div { --x: calc(var(--x) + 1); } The above is…
Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
11
votes
3 answers

CSS Variables and Angular 5

I'm trying to use css varible in my Angular application, inside a child component, and it's just not working. In the browser's Elements tool (I'm using Chrome latest version) the var() doesn't do anything. expamle: css file: :root { --width:…
TalGabay
  • 238
  • 2
  • 9
11
votes
1 answer

Unset/Delete a custom property/CSS variable

Take the following example: .article, .note { color: var(--text-color, red); } .theme { --text-color: blue; } .theme .note { --text-color: unset; }

Article

Philipp Mitterer
  • 863
  • 9
  • 16
11
votes
3 answers

How to Assign CSS Variable Value to scss Variable or Expression

I'm trying to build my own tiny scalable grid in CSS / scss. So far I found this decision: :root { --page-width: 1170px; --gutter: 15px; --columns: 12; } .wrapper { max-width: var(--page-width); margin-right: auto; margin-left: auto; …
Andrii.Gaidai
  • 133
  • 1
  • 1
  • 8
11
votes
1 answer

CSS Variable arrays - possible?

I understand how to use variables in CSS. They have helped heaps with speeding up my work flow for my clients' sites. I have noticed on more than a few occasions I would write down variables with a number next to it to assign to a particular color…
DeveloperDoug
  • 121
  • 1
  • 3
10
votes
4 answers

Using CSS @Variables

I am trying to use CSS Variables. I look online for tutorials and all of them worked so far. Here's my CSS: @variables { defaultColor: #8E5050; defaultBackGround: #E1DBC3; } body { background: var(defaultBackGround); } a { color:…
Tech4Wilco
  • 6,740
  • 5
  • 46
  • 81
10
votes
1 answer

How to list all css variables names/values pairs from element

I have JS library and I have this issue: I'm creating temporary element for calculating size of character using monospace font. Right now I'm copying inlie style, but I need all styles from original including css variables. I don't want to clone the…
jcubic
  • 61,973
  • 54
  • 229
  • 402
10
votes
2 answers

Unable to overwrite CSS variable with its own value

Lets assume to have the following CSS: .box { width: var(--box-size); height: var(--box-size); border: 1px solid red; } .box--larger { --box-size: 66px; } .box--larger1 { --box-size: calc(var(--box-size) +…
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
10
votes
1 answer

Is it possible to use css variables in a svg background-image?

CSS variables don't work when used in an SVG background-image: .icon-24 { width: 24px; height: 24px; display: inline-block; vertical-align: middle; background-repeat: no-repeat; background-color: silver; } .icon-24-stroke…
François Romain
  • 13,617
  • 17
  • 89
  • 123
10
votes
5 answers

List CSS custom properties (CSS Variables)

I've set some CSS custom properties in my stylesheet: :root { --bc: #fff; --bc-primary: #eee; --bc-secondary: #ddd; } I can retrieve them individually if I already know the name of the CSS variable like…
MorganR
  • 619
  • 3
  • 11
  • 22