×

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

Related Topics

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS 2d Transform

Transform property in CSS is used mainly for enhancing the effects and to animate the elements. Transform is an effect that changes the shape, size and position of an element. It helps...

6 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

How to Set Space between the Flexbox

For managing layout on the Web, CSS Flexbox and CSS Grid are two excellent tools. Flexbox handles one-dimensional layouts quite well, whereas CSS Grid manages two-dimensional layouts with columns and...

4 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

How to center a table in CSS

The table is used to store and arrange the data in tabular format. The data here can be of any form like text, image, links, etc. The table is made...

3 minutes read.

Radial-gradient() CSS function

Radial-gradient() CSS function It defines a CSS in-built function, which produces a smooth transition among multiple colors. It is used to set any radial-gradient that is same as a background-image. This...

4 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 minutes read.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

Difference between HTML and CSS

HTML HTML stands for HyperText Markup Language and is used to create web pages and websites and web applications, which means we can create static web pages. Html is a scripting...

3 minutes read.