Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: var(--tintColor);
}

When the variable isn't set like if its commented out the color becomes black. I want it in this case that the color falls back to the blue when the variable isn't defined. Is this possible?

like image 254
swift-lynx Avatar asked Apr 17 '19 15:04

swift-lynx


People also ask

In which situation might it be useful to use CSS variables?

CSS variables, more accurately known as CSS custom properties, are landing in Chrome 49. They can be useful for reducing repetition in CSS, and also for powerful runtime effects like theme switching and potentially extending/polyfilling future CSS features.

Can you scope CSS variables?

First of all: CSS variables can have a global or local scope. Global variables can be accessed/used through the entire document, while local variables can be used only inside the selector where it is declared. To create a variable with global scope, declare it inside the :root selector.

Can we override CSS variables?

Moreover, these have either a global scope or a local scope and can be accessed using the var() function. You can avoid writing down repeated CSS values by using these variables, furthermore, these are easier to understand. It is also possible to override CSS variables with one another.

Can CSS store variables?

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.

What are CSS variables and how to use them?

CSS variables are custom variables that you can create and reuse throughout your stylesheet. In this article, I will show you how to create CSS variables on the :root pseudo selector and show you how to access them using the var () function. It is best practice to define all of your variables at the top of your stylesheet.

How to insert the value of a CSS Variable?

The var () function is used to insert the value of a CSS variable. The syntax of the var () function is as follows: var ( name, value) Value. Description. name. Required. The variable name (must start with two dashes) value.

What is the use of Var in CSS?

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. 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);).

How do you declare a variable in CSS?

The var() Function. Variables in CSS should be declared within a CSS selector that defines its scope. For a global scope you can use either the :root or the body selector. The variable name must begin with two dashes (--) and is case sensitive!


Video Answer


1 Answers

You can specify the fallback property like var(--tintColor, blue) - see demo below:

.text {
    color: blue;
    color: var(--tintColor, blue);
}
<div class="text">some text here</div>
<div class="text" style="--tintColor: red">some text here</div>
like image 191
kukkuz Avatar answered Oct 02 '22 03:10

kukkuz