×

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 Button Style

CSS button style: Using CSS properties, we can easily modify the default BUTTON and enhance its appearances using simple graphic commands. TYPES OF BUTTON STYLE: - Program to change button using CSS ...

35 minutes read.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 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.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

Untitled Reusable Block

  ...

1 minute read.

CSS Text-stroke

Text-stroke Text-stroke CSS property inserts the stroke to any text and gives a decoration option to them also. It describes the stroke’s width and color for various text characters. It is...

2 minutes read.

CSS Text-Overflow

Introduction: The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the...

5 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Text-decoration

CSS Text-decoration: It is an essential property of CSS that is used to decorate the content of the text. It can add-on the lines above, under, and over the text....

3 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

CSS Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

2 minutes read.