Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
CSS | Shorthand Properties
Next article icon

What are CSS Custom Properties ?

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn about CSS custom properties & their implementation. CSS custom properties are also referred to as CSS variables or cascading variables. These entities contain specific values that will be reused in the entire document & can be accessed using the var() function (e.g., color:(--primary-color);). The property name which has a prefix as --, depicts the custom properties & their value will be used in other declarations using the var() function.

Generally, when we are styling a large website that may contain a huge amount of CSS properties & most probably we have used similar values multiple times. It is really hard to manage the large-size styling properties. In order to handle this, custom properties allow us to declare a variable in one place, can be referred to multiple other places. This will reduce the effort to manage the code because we can update values easily by making changes in a single place.

Syntax:

  • For variable:
var( --custom-name, value )


Parameters: The var( ) function accepts two parameters which are listed below:

  • --custom-name: It is a required parameter that accepts the custom property name.
  • value: It is an optional parameter. It accepts a fallback value which is used when the custom property is invalid.

Syntax:

  • For custom properties:
--custom-name: values;

The value assigned to the custom property will be matched for any sequence of one or more tokens until the sequence breaks & does not contain any more valid tokens.

We will create some CSS variables & define the value of these CSS variables, use them to style the HTML components. 

Example 1: In this example, we will create CSS variables for background color, primary font color, and heading property. 

HTML
<!DOCTYPE html> <html>   <head>     <style>               /* Declare CSS custom properties          or CSS variables */         :root {             --background-color: #dee1e3;             --primary-font-color: #302AE6;             --heading-color: #14ac60;             --link-color: #093aeb;         }              body {             background-color: var(--background-color);         }              h2 {             color: var(--heading-color);         }              p {             color: var(--primary-font-color);         }              a {             color: var(--link-color);         }     </style> </head>   <body>     <h2>GeeksForGeeks</h2>     <p>A Computer Science portal for geeks.</p>       <a href="http://geeksforgeeks.org">click here</a> </body>   </html> 

Output:

Example 2: In this example, we will use the box-shadow-color property to create the shadow effect while hovering on it.

HTML
<!DOCTYPE html> <html>  <head>     <style>         body {             background-color: #abb0b3;             color: #0f5e1b;         }          #click {             border-radius: 50px;             display: inline-block;             font-size: 35px;             padding: 10px;             transition: box-shadow 1.2s;         }          #click {             box-shadow: 0 0 40px var(--box-shadow-color);             --box-shadow-color: palegoldenrod;         }          #click:hover {             --box-shadow-color: #57ab57;         }     </style> </head>  <body>     <h4>Hover me to see the effect</h4>     <h2 id="click">GeeksforGeeks</h2> </body>  </html> 

Output:

The first output appears on the output screen and the second output shows when it click to the button


Example 3: To understand the importance of CSS custom properties, we will create a web page where we change the theme of the container just by using CSS custom properties along with some normal JavaScript. 

First, we will define some styling properties for the dark theme and the light theme. By default, we set the theme of the container as dark. We will also use theme CSS custom properties to set the style for our HTML webpage elements. 

HTML
<!DOCTYPE html> <html>  <head>     <style>         :root {             --background-color: #000;             --primary-font-color: #302AE6;             --heading-color: #14ac60;             --link-color: #ae2ab3;         }          /* Light theme */         [theme="light"] {             --background-color: #eee;             --primary-font-color: #000;             --heading-color: #303030;             --link-color: #302AE6;         }          .container {             background-color: var(--background-color);             height: 400px;             width: 300px;             border: 2px solid green;         }          h1 {             color: var(--heading-color);         }          p {             color: var(--primary-font-color);         }          a {             color: var(--link-color);         }     </style> </head>  <body>     <center>         <div class="container">             <h1>GeeksForGeeks</h1>              <p>A Computer Science portal for geeks.</p>              <a href="http://geeksforgeeks.org">click here</a>         </div><br>         <label class="checkbox-theme" for="checkbox">             Check the checkbox to change theme             <input type="checkbox" id="checkbox" />         </label>     </center>      <script>         const Switch = document.querySelector(             '.checkbox-theme input[type="checkbox"]');          /* Function to change theme*/         function changeTheme(e) {             if (e.target.checked) {                 document.documentElement                     .setAttribute('theme', 'light');             }             else {                 document.documentElement                     .setAttribute('theme', 'dark');             }         }         Switch.addEventListener(             'change', changeTheme, false);     </script> </body>  </html> 

Output:

While click to checkbox button, you get the changes on element -


Supported Browsers:

  • Google Chrome 49.0
  • Microsoft Edge 15.0
  • Firefox 31.0
  • Safari 9.1
  • opera 36.0

Next Article
CSS | Shorthand Properties
author
vikashgautam11
Improve
Article Tags :
  • Web Technologies
  • CSS
  • CSS-Questions

Similar Reads

  • What are CSS Preprocessors?
    CSS preprocessors are used to write styles in a special syntax that is then compiled into standard CSS. They extend the functionality of standard CSS by introducing features like variables, nesting, mixins, and functions. By using preprocessors, you can take advantage of advanced features that are n
    3 min read
  • Primer CSS Details Custom Caret
    Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by
    2 min read
  • CSS Cursor Property
    CSS Cursor is used to specify the mouse cursor to be displayed while pointing to an element. This property is defined with zero or more <url> values that are separated by commas followed by 1 keyword value as mandatory & each <url> will point to the image file. Syntax: cursor: value;
    3 min read
  • What are the usage of "table-layout" property in CSS ?
    In this article, we are going to learn the usages of the table-layout property in CSS, The table-layout property in CSS specifies the layout of the table which consists of table cells, rows, and columns. It can have two values: "auto," which allows the table to adjust its layout based on content, an
    3 min read
  • CSS | Shorthand Properties
    Shorthand properties allow us to write multiple properties in a single line and in a compact way. They are useful as they provide clean code and also decrease the LOC (Line of Code). The Shorthand properties we will be covering: BackgroundFontBorderOutlineMarginPaddingList Background: The CSS Backgr
    4 min read
  • SVG Properties In CSS
    SVGs are XML-based vector images that are widely used on the web. It is not made of pixels. SVGs are composed of paths defined by mathematical equations. This allows them to scale infinitely without losing quality. We will learn What are SVG properties in CSS and how to work with SVG properties in C
    7 min read
  • CSS border-left-width Property
    The border-left-width property in CSS specifies the width of an element's left border. You can set this width using various units like pixels, ems, or percentages. This property allows for precise control over the left border's thickness, contributing to the element's overall design and layout. Synt
    4 min read
  • CSS border-right Property
    The border-right Property is a shorthand property used for all the three Properties that are given below- border-right-widthborder-right-style(if required)border-right-colorSyntax: border-right: border-width border-style border-color|initial|inherit;Property Values:  border-width border-style border
    3 min read
  • CSS min-width Property
    The min-width property in CSS is used to define the minimum width of an element. The value of the width cannot be less than the value of the min-width. If the content specified within the element is smaller, min-width maintains the specified minimum width.  Syntax: min-width: length|initial|inherit;
    3 min read
  • What are custom attributes in HTML5 ?
    Custom attributes are attributes that are not part of the standard HTML5 attributes but are explicitly created. They allow us to add our information to HTML tags. These attributes store private information specific to a page or application, providing a way to add custom data to HTML elements Any att
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences