CSS VARIABLES : Custom Properties

 WHAT ARE CSS VARIABLES? 

   Let us break this down for our newbies. Let's start from VARIABLES i.e the head-word. Variables are memory locations for storing data. They act as containers for holding different types of information, such as numbers, text, or more complex data structures. Variables are an essential concept in programming and are used to perform various tasks, calculations, and data manipulation. Wow!

   Now CSS, this stands for Cascading Style Sheet. It is used for styling and designing your web page. It works hand in hand with HTML! The current version is CSS3.

   CSS Variables, also known as Custom Properties, is a feature in CSS that allows you to define reusable values and use them throughout your stylesheet. They provide a way to make your CSS more maintainable and dynamic by centralizing values in one place, which can be especially helpful for theming and responsive design.

   

Defining CSS Variables:

To define a CSS variable, use the -- prefix followed by a name and assign a value to it. Because every variable should have a name! For example:

css

:root {

  --main-bg-color: #3498db;

  --text-color: #ffffff;

}


Here, we define two variables:

 --main-bg-color and --text-color.


Using CSS Variables:

You can use CSS variables within your styles by referencing them with the var() function. For example:

css

body {

  background-color: var(--main-bg-color);

  color: var(--text-color);

}

Now, this will set the background color for the body element to be #3498db



Dynamic Updating:


One of the most powerful features of CSS variables is their ability to dynamically change values with JavaScript. You can modify the values of CSS variables using JavaScript, and it will instantly update the styles across your webpage.


Fallback Values:


You can provide fallback values for CSS variables by using a second argument in the var() function. For example:

css

color: var(--text-color, #000000); / Fallback to #000000 if --text-color is not defined /



Scope:

CSS variables are scoped to the element they are defined on and can be inherited by child elements.


Use Cases:

1. Theming: Easily switch between light and dark themes by changing variable values.

2. Responsive Design: Adjust layout and sizing based on screen size.

3. Consistency: Maintain a consistent design language by reusing colors, fonts, and other values.


Browser Support:

CSS variables are well-supported in modern browsers, but you may need to provide fallbacks for older browsers.


Example:

css

:root {

  --primary-color: #007bff;

}


body {

  background-color: var(--primary-color,#111111);

}


button {

  background-color: var(--primary-color,#111111);

  color: #ffffff;

}



Conclusion:

CSS variables provide a powerful way to manage and maintain your styles, making your CSS more modular and adaptable. They are a valuable tool for creating consistent and dynamic web designs. They can also be used with other functions like THE calc() function! 


Comments

Popular posts from this blog

GreX Tutorial : Hello World in HTML

GreX Tutorial : WEB DEVELOPMENT

GreX Tutorial : Introduction to HTML