Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Using CSS Custom properties (variables)
Arbaoui Mehdi
Arbaoui Mehdi

Posted on

     

Using CSS Custom properties (variables)

Video Lesson:

Code Used in this lesson:

https://github.com/freemh/dont-repeat-your-css/tree/master/1-sass-fundamentals/3-css-variables

Introduction

Variables are used to store some specific values, and we can use them whenever you need them, CSS language has already the capability to use variables without the need of a preprocessor like Sass, and these CSS variables or they call them Custom Properties they have some features that Sass Variables doesn’t have, like access to the DOM, because they are considered a part of the DOM, we can even control them using JavaScript. Controlling CSS variables using javascript is not our subject today, we’ll use them from a simplemain.css file.

How to create a CSS Variable?

To create a CSS variable is very simple, but first, we need to understand that we’ve two places where we store our variables, the first one is considered as global, and if we create a variable in the global scope or closure it will be accessible within the whole page.

Let’s create a variabledanger-700 that will be accessible within the global scope by using the:root pseudo-class, then to create a variable we use these two dashes and the name of the variable--danger-700 in this case, then we set a value to it normally, like setting a value to a property, we go for a hex color#721c24, which it’s already here.

:root{--danger-700:#721c24;}
Enter fullscreen modeExit fullscreen mode

This700 here on the variable name means nothing, it’s just a convention that defines that this shade of the color is the darkest one.

Let’s replace this color#721c24 from this style sheet with the variable--danger-700, we’ve one here at.alert-danger, and to set the variable--danger-700 to thecolor property we need to use this functionvar(), then inside of the function we add the variable--danger-700, this is the only way on how to set a variable to a property within CSS.

.alert-danger{color:var(--danger-700);}
Enter fullscreen modeExit fullscreen mode

Same goes for the.btn-danger selector

.btn-danger{background:var(--danger-700);}
Enter fullscreen modeExit fullscreen mode

and the:hover pseudo-class of the.btn-danger, we replace this value#721c24 byvar(--danger-700), COOL.

.btn-danger:hover{border:1pxsolid#f5c6cb;background-color:#f8d7da;color:var(--danger-700);}
Enter fullscreen modeExit fullscreen mode

We can do the same thing, for the other colors “#f5c6cb“#f8d7da, we create two other variables “danger-200and “danger-100.

--danger-200:#f5c6cb;--danger-100:#f8d7da;
Enter fullscreen modeExit fullscreen mode

And we find these values, and replace them within the file, for both.alert-danger selectors

.alert-danger{border:1pxsolidvar(--danger-200);background-color:var(--danger-100);}
Enter fullscreen modeExit fullscreen mode

And the.btn-danger:hover pseudo-class.

.btn-danger{background:var(--danger-700);}.btn-danger:hover{border:1pxsolidvar(--danger-200);background-color:var(--danger-100);}
Enter fullscreen modeExit fullscreen mode

To understand what this:root it is exactly, so here on theindex.html page we have nested tags, and the parent of all of them is the ‘’ tag, and this is what:root is about, when we create a variable within the:root, any nested element will be able to use this variable, it’ll be considered global.

Now if we’re in the opposite, and we want to create a variable that will be only accessible within a specific block or scope, Okay, we’ll go for thisalert element, the goal is to create a variable that can be used only within this closure, not anywhere else.
To achieve that, we’ve to create the variable within the parent which it’s alert.
Okay, let’s move to ourmain.css file, and from.alert we’ll create a new variable--alert-link and we’ll get its value from the.alert-danger .alert-link.

.alert{--alert-link:#c73c49;}
Enter fullscreen modeExit fullscreen mode

Then, we’ll replace thecolor value from.alert-danger .alert-link selector, with the variable--alert-link

.alert-danger.alert-link{color:var(--alert-link);}
Enter fullscreen modeExit fullscreen mode

Now, if we copy this variable herevar(--alert-link);, and we try to use it from thecolor property of the.btn selector.

.btn{color:var(--alert-link);}
Enter fullscreen modeExit fullscreen mode

It’ll not work, why because the variable--alert-link can only be used within its scope, not anywhere else.
Let’s return the button color initial value.

.btn{color:#fff;}
Enter fullscreen modeExit fullscreen mode

If we want to use the variable--alert-link within the.btn selector, we’ve two choices, whether create it on the:root which will be global or create another variable--alert-link within the.btn selector.

CSS Variables or Sass Variables

I’m sure that you’re asking yourself, so which one to use, the Sass variables, or CSS variables, the answer is depending on the use case, because CSS variables are not 100% supported by all the browsers, and a simplehttps://caniuse.com/css-variables will show some red colors, which means that it’s not fully supported, so you’ve to know them both, and the project of one of your client will have more power in this situation on which environment he or she wants the website to be used.

But stick to the Sass variables as the first choice, and if cross browsers validation it’s not something you’ve to deal with, in this case, you can use CSS variables.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
nuxodin profile image
Tobias Buschor
  • Joined
CollapseExpand
 
arbaoui_mehdi profile image
Arbaoui Mehdi
JavaScript Coder, CSS Lover, Online Instructor 📚
  • Location
    Casablanca, Morocco
  • Work
    Frontend engineer and Online Instructor
  • Joined

Thanks for noticing that.

CollapseExpand
 
adam_cyclones profile image
Adam Crockett 🌀
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
  • Location
    City of Bath, UK 🇬🇧
  • Education
    11 plus years* active enterprise development experience and a Fine art degree 🎨
  • Work
    Web Development Consultant at ForgeRock
  • Joined

This is only half the story because setting and getting CSS variables with js is where the true power lies.

CollapseExpand
 
arbaoui_mehdi profile image
Arbaoui Mehdi
JavaScript Coder, CSS Lover, Online Instructor 📚
  • Location
    Casablanca, Morocco
  • Work
    Frontend engineer and Online Instructor
  • Joined

Yeah, for sure this just an entry-level to CSS custom properties, just to have an idea on how to use them.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

JavaScript Coder, CSS Lover, Online Instructor 📚
  • Location
    Casablanca, Morocco
  • Work
    Frontend engineer and Online Instructor
  • Joined

More fromArbaoui Mehdi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp