CSS Introduction

CSS Tutorial What is CSS CSS Syntax CSS Selector How to include CSS CSS Comments

CSS Attributes

CSS Background CSS Border CSS Display CSS Float CSS Font CSS Color CSS Hover CSS Important CSS Line-height CSS Margin CSS Opacity CSS Filter CSS Images CSS Overflow CSS Padding CSS Position CSS Vertical align CSS White space CSS Width Word-wrap in CSS Box-shadow in CSS Text-transform in CSS CSS Outline CSS Visibility CSS Counters CSS Clear fix CSS Icons CSS Justify-content Text-decoration in CSS CSS Lists CSS nth selector CSS Sticky CSS Background-clip CSS Checkbox-style CSS Letter-spacing CSS Navigation bar CSS Overlay CSS Root CSS Specificity CSS Text-indent CSS Text-stroke CSS Zoom CSS Order CSS Descendent selector CSS Clip CSS calc() CSS Background-blend-mode CSS radio-button CSS Superscript and subscript CSS Text-effects CSS Text-align CSS Variables CSS Page-break-before CSS Page-break-inside CSS Page-break-after CSS Content property CSS Word-spacing CSS Animation CSS @keyframes rules CSS Pseudo-classes CSS Pseudo-elements CSS Radial-gradient CSS Translate CSS Gradients CSS z-index CSS Loaders CSS Units CSS Transition CSS Masking CSS Arrow CSS Pagination

Questions

What is Bootstrap CSS What is CSS used for How to center a table in CSS What is a CSS File How to center a button in CSS How to change background color in CSS How to change the font in CSS How to change font size in CSS How to resize an image in CSS How to get rid of bullet pioints in CSS Is CSS a programming language How to edit CSS in WordPress How to use google fonts in CSS How to create blinking text using CSS How to Set Space between the Flexbox Is CSS a Programming Language

Difference

Difference between HTML and CSS Grid Vs Flexbox in CSS Difference between CSS Grid and CSS Flexbox

Misc

Create a 3D text effect using HTML and CSS Hover condition for a:before and a:after in CSS Bem CSS Boder Types CSS Features of CSS Font Face CSS Image Overlay CSS B Tag CSS Carousel CSS CSS Arrow CSS Focus Flex Grow in CSS Bem CSS Features of CSS Font Face CSS Removing Underline from Link in CSS Width Property in CSS Radio Button in CSS

CSS Variables

CSS Variables

These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various authors portray these items because they include specific values or may be recycled throughout any document. These items are set with the use of the notation of custom property and may be authorized with the help of var() function.

These variables reserve multiple values, and contain a scope where it can be used.

The variables of CSS are advantageous as they permit to reuse any similar value over multiple positions. The variable name is efficient to use and understand, unlike the value of the color.

Syntax:

element
{
--main-color: brown;  
}  

A CSS variable is specified with the use of two dashes (--) in the starting, pursued by a case-sensitive name.

The element shows a selector that describes the custom property’s scope, in the above-described syntax. If we depict a custom property over a pseudo-class: root, this class will be applied to the HTML document globally. The custom properties names are case-sensitive, like --Main-color and --main-color will be evaluated as a separate custom attribute.

var() function

This CSS function is applied to add the value of the custom property. The variable name may be passed the same as an argument to a var() function.

Syntax:

var( --custom-name, value )  

Parameters

The function var() allows only two arguments as below:

--custom-name: It accepts the custom property name. This parameter should start with two dashes (--). This parameter is a required parameter.

Value: This parameter is the optional parameter. It allows for any fallback value. If any custom property will be invalid, then this parameter will be used as a substitution.

The fallback value is not applied to fix-up the compatibility of the browser. These values are not advantageous when the browsers do not provide their support to any custom properties. This value implements as a substitution for any browser, i.e., offers its support for custom properties of CSS for selecting any distinct value if a variable isn’t depicted or if any variable contains any invalid value.

A few invalid or valid ways to illustrate these fallback values are mentioned as below:

element {  
 color: var(--main-color, orange); /* orange if --main-color is not defined */  
}  
element {  
 background-color: var(--main-color, var(--main-background, blue)); /* blue if --main-color and --main-background are not defined */  
}  
element {  
 background-color: var(--main-color, --main-background, gray); /* Invalid*/  
}  

Let’s take some illustration to understand the concept of CSS variables.

Example:

<!DOCTYPE html>
<html>
<head>
<title> CSS Variables </title>
<style>
:root
{
 --bg-color: lightblue;
 --text-color: tomato;
}
/* Here var() function is used */
body
{
 background-color: var(--bg-color);
 text-align: center;
}
h1
{
 color: var(--text-color);
}
div
{
 color: var(--text-color);
 font-size: 30px;
}
</style>
</head>
<body>
<h1> Welcome to this Page </h1>
<div>
It is an illustration of the CSS variables.
<h3> --bg-color: lightblue; </h3>
<h3> --text-color: tomato; </h3>
</div>
</body>
</html>

Output:

CSS Variables

The above example is not defined with the fallback values. Consider the below example:

Example:

In the following demonstration, here we use –text-color CSS variable. These fallback values are used as a variable substitution because the variable –text-color is not set.

<!DOCTYPE html>
<html>
<head>
<title> CSS Variables </title>
<style>
:root
{
 --bg-color: lightblue;
}
body
{
 background-color: var(--bg-color);
 text-align: center;
}
h1
{
 color: var(--text-color, navy); /*fallback value navy will be applied because the --text-color is not set.*/
}
div
{
 color: var(--text-color, navy);
 font-size: 30px;
}
</style>
</head>
<body>
<h1> Welcome to this Page </h1>
<div>
It is an illustration of the CSS variables.
<h3> --bg-color: lightblue; </h3>
</div>
</body>
</html>

Output:

CSS Variables

var() with the Calc()

We can apply the calc() function over the values of the variables. In the following demonstration, we are using the var() function with the calc() function.

We are applying the var() function with the calc() function to maintain the font-size and padding of any element.

Example:

<!DOCTYPE html>
<html>
<head>
<title> CSS Variables </title>
<style>
:root
{
 --bg-color: lightblue;
 --extra-padding: 1.2em;
 --txt-size: 90px;
}
body
{
 background-color: var(--bg-color);
 text-align: center;
}
h1
{
 color: var(--text-color, navy); /*fallback value navy will be applied because the --text-color is not set.*/
 font-size: calc(var(--txt-size) - 20px);
}
div
{
 color: var(--text-color, navy);
 font-size: 30px;
 border: 8px ridge red;
 padding: calc(var(--extra-padding) + 20px);
}
</style>
</head>
<body>
<h1> Welcome to this Page </h1>
<div>
Here, we are using the var() function with the calc() function.
</div>
</body>
</html>

Output:

CSS Variables