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
521
votes
18 answers

How do I apply opacity to a CSS color variable?

I am designing an app in electron, so I have access to CSS variables. I have defined a color variable in vars.css: :root { --color: #f0f0f0; } I want to use this color in main.css, but with some opacity applied: #element { background: (somehow…
jotch
  • 5,416
  • 2
  • 12
  • 20
324
votes
10 answers

CSS native variables not working in media queries

I am trying to use CSS variables in media query and it does not work. :root { --mobile-breakpoint: 642px; } @media (max-width: var(--mobile-breakpoint)) { }
user7122183
274
votes
19 answers

How can I define colors as variables in CSS?

I’m working on a CSS file that is quite long. I know that the client could ask for changes to the color scheme, and was wondering: is it possible to assign colors to variables, so that I can just change a variable to have the new color applied to…
patrick
  • 7,369
  • 5
  • 20
  • 14
165
votes
2 answers

Access CSS variable from javascript

Is there a way to access a css variable from javascript? Here my css variable declaration. :root { --color-font-general: #336699; }
Pablo
  • 2,581
  • 3
  • 16
  • 31
136
votes
4 answers

Is there a way to interpolate CSS variables with url()?

I want to store my background URLs in custom properties (CSS variables) and use them with the background property. However, I couldn't find a way to interpolate the string when using it as a parameter in url(). Here is my sample code: :root { …
YashArora
  • 1,473
  • 2
  • 9
  • 8
81
votes
1 answer

Using negative CSS Custom Properties

In preprocessors, like SASS, you can use negative values like: $margin-md: 10px; .class { margin-bottom: -$margin-md; } How do I do this using custom properties? // This doesn't work .class { margin-bottom: -var(--margin-md); }
bholtbholt
  • 11,281
  • 6
  • 22
  • 32
78
votes
4 answers

Accessing a CSS custom property (aka CSS variable) through JavaScript

How do you get and set CSS custom properties (those accessed with var(…) in the stylesheet) using JavaScript (plain or jQuery)? Here is my unsuccessful try: clicking on the buttons changes the usual font-weight property, but not the custom --mycolor…
MarcZ
  • 803
  • 1
  • 5
  • 5
52
votes
4 answers

How to create color shades using CSS variables similar to darken() of Sass?

I'm looking a way of modifying a CSS variable as you would in SCSS Define a color like primary - and automatically I would get shades for focus and actives states. Basically, would like to change one variable in css variables and get 3 shades of…
Daniel
  • 815
  • 1
  • 6
  • 13
51
votes
4 answers

CSS Variables (custom properties) in Pseudo-element "content" Property

Example use (what I want) div::after { content: var(--mouse-x) ' / ' var(--mouse-y); } Test case showing it NOT working: CodePen: CSS Variables in Pseudo Element's "content:" Property (a test case) by Jase…
Jase
  • 603
  • 1
  • 6
  • 7
50
votes
2 answers

How do you inspect CSS variables in the browser?

I am defining my variables as per the spec like so: :root { --my-colour: #000; } And accessing them like this: .my-element { background: var( --my-colour ); } Which works fine. However I was wondering if there was a way of debugging or…
Paul. B
  • 1,328
  • 1
  • 13
  • 22
44
votes
3 answers

CSS Variables with background-image url

Am I misunderstanding the capabilities of css variables? I am trying to pass a background image url to a variable like this: It seems to be working fine when I pass something simple like a color etc... :root { --slide-1:…
Sandra Willford
  • 3,459
  • 11
  • 49
  • 96
43
votes
3 answers

How can I get a negative value of CSS variables (aka Custom Properties)

Let me start by showing you how I would do this in SCSS: $submenu-padding-left: 1.5em; transform: translateX(calc(-#{$submenu-padding-left} + .5em)); which would compile to: transform: translateX(calc(-1.5em - .5em)) Basically SCSS allows me to…
IOIIOOIO
  • 3,899
  • 4
  • 14
  • 19
42
votes
1 answer

Are CSS Custom Properties global across linked CSS documents?

I'm experimenting with a lot of success with CSS3 Custom Properties (aka CSS Variables). I'm talking about the --black: #000; and background: var(--black); type variables. However, I'm having no luck when variables are declared in separate linked…
Dom
  • 3,173
  • 4
  • 30
  • 39
38
votes
8 answers

Any way to use CSS Variables in SASS functions for hsl() transparency?

I have defined a color in my root: :root { --purple: hsl(266, 35%, 70%); } And I'm trying to use it in a SASS function to give it transparency: .purple { background: transparentize(#{"var(--primary-color)"}, 0.7) } Does anyone know of a way to…
sammiepls
  • 1,340
  • 2
  • 13
  • 19
35
votes
2 answers

Effect of Many CSS Variables on Performance

This is a somewhat general question. Is anybody aware of the effects on performance of using many CSS variables within a given document? Has anybody done any tests? Does the element to which you associate the variable have any effect on performance?…
oldboy
  • 5,729
  • 6
  • 38
  • 86
1
2 3
41 42