The var() function in CSS is used to insert a value for custom property.
Syntax:
var( custom_property, value )
Parameters: This function accepts two parameters which are listed below:
- custom_property: It is the required parameter. The name of custom property must start with two dashes(–).
- value: It is optional parameter. It is used if custom property is invalid.
Below program illustrates the var() function in CSS:
Program:
<!-- HTML code to describes var() function in CSS --><!DOCTYPE html> <html> <head> <title>var() function</title> <style> :root { --main-bg-color: Green; } /* Use of var() function */ .gfg1 { background-color: var(--main-bg-color); padding:10px; } .gfg2 { background-color: var(--main-bg-color); padding: 5px; } h1 { color:green; } body { text-align:center; } </style> </head> <body> <h1>GeeksforGeeks</h1> <h2>var() function</h2> <div class = "gfg1">GeeksforGeeks</div><br> <div class = "gfg2">A computer science portal for geeks</div> </body> </html> |
chevron_right
filter_none
Output:

Supported Browsers: The browser supported by var() function are listed below:
- Google Chrome 49.0
- Edge 15.0
- Firefox 31.0
- Safari 9.1
- Opera 36.0
Recommended Posts:
- HTML | <var> Tag
- PHP | var keyword
- What is the use of curly brackets in the `var { ... } = …` statements ?
- How to override the CSS properties of a class using another CSS class ?
- Difference between bootstrap.css and bootstrap-theme.css
- CSS | rgb() Function
- CSS | rgba() Function
- CSS | hsla() Function
- CSS | hsl() Function
- CSS | cubic-bezier() Function
- CSS | calc() Function
- CSS | attr() Function
- CSS | linear-gradient() Function
- CSS | radial-gradient() Function
- CSS | repeating-radial-gradient() Function
- CSS | repeating-linear-gradient() Function
- CSS | animation-timing-function Property
- How to remove style added with .css() function using JavaScript?
- CSS | brightness() Function
- CSS | blur() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

