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
21
votes
4 answers

CSS custom properties polyfill for ie11

Is there a way to pollyfill a custom CSS property for ie11 with JavaScript? I was thinking on load, check if browser supports custom properties and if not do some kind of find and replace on the properties. Is this possible with JavaScript or some…
user973671
  • 1,620
  • 6
  • 27
  • 39
21
votes
2 answers

Using CSS Variables as Sass function arguments

Is there a way to use CSS variables with Sass functions e.g. lighten? Something like this: :root { --color: #ff00ff; } .div { background-color: lighten(var(--color), 32%); } I'm getting an error message that "Argument $color of lighten($color,…
Majka
  • 291
  • 3
  • 5
20
votes
2 answers

Svelte: Is there a way to make global css variables in scope of svelte components?

I have set my global.css file which I import in index.js --root { --main-color: red; } * { margin: 0; padding: 0; box-sizing: border-box; } index.js import "./global.css"; import App from "./App.svelte"; const app = new App({ target:…
frank3stein
  • 706
  • 2
  • 7
  • 16
19
votes
3 answers

How to change tailwind-config.js dynamically based on user settings in rails

I have a Rails 6 app set up to use Tailwind CSS with Webpacker similarly to how it's done in this GoRails tutorial. I want to be able to change the Tailwind defaults dynamically based on the controller and action so that it's very easy for users to…
Lee McAlilly
  • 9,084
  • 12
  • 60
  • 94
18
votes
1 answer

How to use a unitless CSS variables and later add the needed unit?

I am trying to set a CSS variable with a number then I want to use it in my CSS as a percentage, while in other places I will need to use it as a plain number for some calc() operations. Example --mywidth:10; div{width:var(--mywidth) + %;} ---…
Ziv Feldman
  • 321
  • 2
  • 13
18
votes
6 answers

CSS Variables and String concatenation

I'm trying to use CSS variables to generate a dynamic path. Example: :root { --fonts-path: "/path/to/font"; } @font-face { font-family: "FontName"; src: url(var(--fonts-path) + "/FontName-Light.woff") format('woff'); font-weight:…
Alan Souza
  • 7,475
  • 10
  • 46
  • 68
16
votes
2 answers

Can I add a classname to a CSS variable?

Is it possible to add a classname to a CSS variable or is there some other way to set it up so that I don't have to manipulate each individual variable directly via javascript? I'd like to keep all my styles in CSS and simply turn on relevant…
cronoklee
  • 6,482
  • 9
  • 52
  • 80
15
votes
1 answer

How to use CSS Variables only when they exist?

I have some CSS where I want a default color and then override it with a variable. When the CSS Variable doesn't exist, I want to use the fallback color. :root { /*--tintColor: red;*/ } .text { color: blue; color:…
swift-lynx
  • 3,219
  • 3
  • 26
  • 45
15
votes
2 answers

Can I use CSS variables in a font list and have it work in legacy browsers?

I'd like to use a CSS variable to store a font in case the user doesn't have the specified font installed. Example: :root { --common-font: Comic Sans MS; } .header1 { font-family: CoolFont1, var(--common-font); } Will legacy browsers…
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
14
votes
4 answers

CSS variables in Material ui - createMuiTheme

I am trying to create a material ui theme using the existing colors defined as css variables my-pallette.scss: :root { --primary-color: '#FF0000'; ... } And use this like this: import { createMuiTheme } from '@material-ui/core/styles'; export…
Cezar Cobuz
  • 1,077
  • 1
  • 12
  • 34
14
votes
1 answer

On Chrome background images set with CSS custom properties flicker when inside an anchor tag

I am using CSS custom properties to set the background image for a div. The div is nested inside an anchor tag to represent a clickable "card" that routes to another page. In Chrome with dev tools open and cache disabled, when I click on the card,…
hally9k
  • 2,423
  • 2
  • 25
  • 47
14
votes
5 answers

Why can't I animate custom properties (aka CSS variables)?

See this animation: The golden div has an animation where a custom property is animated (@keyframes roll-o-1 animates --o). This animates in steps. The silver div has an animation where a normal property is animated (@keyframes roll-o-2…
yunzen
  • 32,854
  • 11
  • 73
  • 106
14
votes
3 answers

Bootstrap 4 - Reference a color by root name in css

I'm using a nice Bootstrap 4 css theme which overrides default one with very nice colors but I'm not being able to "reference" a color by its root name from css. Lets explain better, Bootstrap has some root colors which names are "success",…
Diego Perez
  • 2,188
  • 2
  • 30
  • 58
13
votes
3 answers

Using css variables in sass functions - node-sass

I am trying to use CSS variables in an Angular project together with Sass functions (lighten() / darken() to be precise), and as far as I know, the latest version of LibSass allows it. I have installed node-sass@4.11.0 and sass-loader@7.1.0 (and…
Dan R.
  • 638
  • 5
  • 13
13
votes
3 answers

CSS custom properties in box-shadow color function render incorrectly in Safari

Is this a Safari bug or am I doing something wrong? Check out this CodePen in Safari vs Chrome and Firefox: https://codepen.io/mattaningram/pen/zWVxzR Or run code snippet below: .item { --itemColor: 200, 0, 0; --itemColorHex: #C80000; …
mattaningram
  • 297
  • 1
  • 5
  • 13
1 2
3
41 42