Learn about CSS Variables Cheat Sheet

Learn about CSS Variables Cheat Sheet

CSS variables are an extremely useful feature and they enable us to do things we were compensating with SASS

Introduction

CSS variables are an extremely useful feature and they enable us to do things we were compensating with SASS. They are defined by the CSS authors and contain a specific values that can be reused wherever needed within our project. They use a custom notation and are accessed by using the var function.

Declaration

A CSS variable can be declared on any element, and we declare one like so:

div {
    --primary-color: green;
}

They are accessed by using the var function:

div {
    color: var(--primary-color);
}
An Example Use Case

Let's define a use case where we define the following CSS rules to our HTML:

.card-wrapper {
    padding: 1rem;
    display: flex;
    flex-direction: column
}
.card {
    border: 1px solid #0068d8;
    margin-bottom: 0.5rem
}
.card-title {
    padding: 1rem;
    background-color: #007bff;
    color: #fff
}
.card-body {
    padding: 1rem;
    background-color: #76b8ff;
    color: #fff
}

and apply them to the following HTML:

<section class="card-wrapper">
    <article class="card">
        <header class="card-title">
            Card 1
        </header>
        <main class="card-body">
            Lorem ipsum dolor sit amet...
        </main>
    </article>
    <article class="card">
        <header class="card-title">
            Card 2
        </header>
        <main class="card-body">
            Lorem ipsum dolor sit amet...
        </main>
    </article>
    <article class="card">
        <header class="card-title">
            Card 3
        </header>
        <main class="card-body">
            Lorem ipsum dolor sit amet...
        </main>
    </article>
</section>

Can you spot the repetitions? After analysing our CSS we come to a conclusion that the following properties are duplicates:

  1. padding
  2. color

Therefore, we can make an advantage of the new CSS feature. We can use CSS variables to define the padding and color for our elements. By declaring them on a :root pseudo class we can overcome this problem and reuse both padding and color wherever we need to. Our updated CSS looks like this:

:root {
    --primary-color: #fff;
    --primary-padding: 1rem
}

.card-wrapper {
padding: var(--primary-padding);
display: flex;
flex-direction: column
}
.card {
border: 1px solid #0068d8;
margin-bottom: 0.5rem
}
.card-title {
padding: var(--primary-padding);
background-color: #007bff;
color: var(--primary-color)
}
.card-body {
padding: var(--primary-padding);
background-color: #76b8ff;
color: var(--primary-color)
}

The HTML remains the same, and after opening the page in your browser, the result should be the same.

Inheritance

CSS variables can inherit its value like any other CSS property. If no value is set for it on an element, the value of its parent is used. Let's update our CSS to reflect this:

:root {
--primary-color: #fff;
--primary-padding: 1rem
}

.card-wrapper {
padding: var(--primary-padding);
display: flex;
flex-direction: column
}
.card {
border: 1px solid #0068d8;
margin-bottom: 0.5rem
}
.card-title {
--primary-padding: 1.5rem;
--primary-color: red;
padding: var(--primary-padding);
background-color: #007bff;
color: var(--primary-color)
}
.card-body {
padding: var(--primary-padding);
background-color: #76b8ff;
color: var(--primary-color)
}

Now, if we run the page in the browser, the result should be that each card header has its padding and color set to 1.5rem and red, respectively, instead of the values defined on the :root pseudo class. This means that all elements have inherited their color and padding from the parent (in this case the :root pseudo class) except of the card header.

Default (fallback) Values

CSS variables do support the default or fallback values, meaning that we can define what value we want to use if the custom CSS variable fails. This is done by passing the second parameter to the var function. The given value will be used as a substitute when the CSS variable is not defined. Let's update the CSS to reflect this:

:root {
/--primary-color: #fff; a fallback value is used instead of this variable/
--primary-padding: 1rem
}

.card-wrapper {
padding: var(--primary-padding, 1rem);
display: flex;
flex-direction: column
}
.card {
border: 1px solid #0068d8;
margin-bottom: 0.5rem
}
.card-title {
--primary-padding: 1.5rem;
--primary-color: red;
padding: var(--primary-padding, 1rem);
background-color: #007bff;
color: var(--primary-color, #fff)
}
.card-body {
padding: var(--primary-padding, 1rem);
background-color: #76b8ff;
color: var(--primary-color, #fff)
}

The output should, again, be the same since we've defined the white color as the fallback value. Play around with it a little bit to see the effect.

Invalid Values

If the CSS variable has been defined with an invalid value the browser will set the initial or the parent value for the given property.

First, it will check if the property is inheritable, if it is, it will take the value from the parent (if the element has one). If the element doesn't have a parent, the default initial value for the property will be used. Let's update the card wrapper and title classes to reflect this behaviour:

...

.card-wrapper {
padding: var(--primary-padding);
display: flex;
flex-direction: column;
color: orange /* assign a new color here /
}
.card-title {
--primary-padding: 1.5rem;
--primary-color: 1px; /
assign an invalid value here */
padding: var(--primary-padding, 1rem);
background-color: #007bff;
color: var(--primary-color, #fff)
}

...

Now, if we run the page in the browser, card headers will have a color changed to orange

Dynamic Usage (JavaScript)

CSS variables can be accessed from the JavaScript dynamically. We can get or set its value like for any other property:

const el = document.querySelector(':root');

// get the value
const padding = el.style.getPropertyValue('--primary-padding');

// set the value
el.style.setProperty('--primary-padding', '3rem');

Thank for reading. Originally published on https://dev.to/

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]

Introduction To HTML & CSS: Web Development For Beginners

Introduction To HTML & CSS: Web Development For Beginners

Introduction To HTML & CSS: Web Development For Beginners - Complete HTML & CSS web developer bootcamp. Learn & practice website design using HTML5 & CSS3 web development resources

Welcome to Introduction To HTML & CSS: Web Development For Beginners: This is a comprehensive and Practical guide that is designed to pick up every interested student from the state of "zero-knowledge" to a state of "Hero-knowledge" in HTML and CSS. As a senior software developer and an instructor with many years of experience in the industry, I have mastered the best way to give my students packages that will help them to learn and understand HTML and CSS in one course.

What you'll learn

  • You will learn the basics of web development
  • You will be able to use the HTML tags professionally
  • You will learn how to work with images in HTML and CSS
  • You will learn how to create awesome backgrounds
  • You will how to use CSS colors
  • You will master most web development documentations used by professionals
  • You will learn how to embed multimedia in web pages
  • You will learn how to make comments in both HTML and CSS
  • You will learn the Box Model