Fireship · @fireship
CSS Variables in 100 Seconds
Uploaded March 19, 2020 · Length 1:56
Summary
reuse css values throughout style sheets with custom properties
- css variables
- custom properties
- css efficiency
- css themes
- css media queries
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
CSS variables, or custom properties, allow you to reuse values throughout your style sheets.
- 0:05
They increase your efficiency, reduce code duplication, and there's all kinds of cool tricks you can do with them, as you'll see in this video.
- 0:10
We can define global variables that will be used throughout the entire website on the root element.
- 0:15
Start with a double dash, followed by whatever name you want to use for the variable.
- 0:18
Then you can use any valid CSS value here.
- 0:21
You can now put the variable to use using the var function.
- 0:23
If you want to be extra safe, you can provide a second argument to var as a fallback value.
- 0:27
fallback value. If text color is undefined or invalid, it will use pink instead. Now when
- 0:31
you use this variable in multiple places, then decide to change the value, you only have
- 0:35
to update it once. Variables cascade downwards, which means you can override them in the children.
- 0:40
If we have a card class and change the text color variable to red, all the children of
- 0:44
a card will use this value instead of the root value. And this means we can change the
- 0:48
appearance of our site by simply tweaking variable values. This concept is especially
- 0:52
powerful when combined with media queries. As the viewport gets smaller, we may want
- 0:55
to decrease the margin between elements.
- 0:58
Instead of updating potentially hundreds of individual CSS classes, we just let our media
- 1:02
query change a variable value, and we can even use the prefers-color-scheme media query
- 1:06
to toggle between a light and dark theme based on the user's device preferences.
- 1:10
Variables also work great with calc.
- 1:12
Define one as a regular number, and then use it to calculate actual pixel values in your
- 1:16
other styles.
- 1:17
Another cool thing is that you're not limited to a single variable in a property value.
- 1:21
Notice how I'm building up a red-green-blue color by using three different variables, and
- 1:25
It's things like this that make CSS variables extremely useful for building themes, because
- 1:29
when we bring JavaScript into the equation, we can change these variables on the fly,
- 1:33
and the browser will automatically repaint the styles that the user sees on the screen.
- 1:37
In our next project video, I'll show you how to build a theme toggler similar to the
- 1:40
one that you see on Alligator.io.
- 1:42
This has been CSS Custom Properties or Variables in 100 Seconds.
- 1:46
Make sure to hit the like button and share this video with your team.
- 1:49
It really helps out these short videos.
- 1:50
Thanks for watching, and I will see you in the next one.


