Fireship · @fireship

CSS Variables in 100 Seconds

Uploaded March 19, 2020 · Length 1:56

CSS Variables in 100 Seconds

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.

  1. 0:00

    CSS variables, or custom properties, allow you to reuse values throughout your style sheets.

  2. 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.

  3. 0:10

    We can define global variables that will be used throughout the entire website on the root element.

  4. 0:15

    Start with a double dash, followed by whatever name you want to use for the variable.

  5. 0:18

    Then you can use any valid CSS value here.

  6. 0:21

    You can now put the variable to use using the var function.

  7. 0:23

    If you want to be extra safe, you can provide a second argument to var as a fallback value.

  8. 0:27

    fallback value. If text color is undefined or invalid, it will use pink instead. Now when

  9. 0:31

    you use this variable in multiple places, then decide to change the value, you only have

  10. 0:35

    to update it once. Variables cascade downwards, which means you can override them in the children.

  11. 0:40

    If we have a card class and change the text color variable to red, all the children of

  12. 0:44

    a card will use this value instead of the root value. And this means we can change the

  13. 0:48

    appearance of our site by simply tweaking variable values. This concept is especially

  14. 0:52

    powerful when combined with media queries. As the viewport gets smaller, we may want

  15. 0:55

    to decrease the margin between elements.

  16. 0:58

    Instead of updating potentially hundreds of individual CSS classes, we just let our media

  17. 1:02

    query change a variable value, and we can even use the prefers-color-scheme media query

  18. 1:06

    to toggle between a light and dark theme based on the user's device preferences.

  19. 1:10

    Variables also work great with calc.

  20. 1:12

    Define one as a regular number, and then use it to calculate actual pixel values in your

  21. 1:16

    other styles.

  22. 1:17

    Another cool thing is that you're not limited to a single variable in a property value.

  23. 1:21

    Notice how I'm building up a red-green-blue color by using three different variables, and

  24. 1:25

    It's things like this that make CSS variables extremely useful for building themes, because

  25. 1:29

    when we bring JavaScript into the equation, we can change these variables on the fly,

  26. 1:33

    and the browser will automatically repaint the styles that the user sees on the screen.

  27. 1:37

    In our next project video, I'll show you how to build a theme toggler similar to the

  28. 1:40

    one that you see on Alligator.io.

  29. 1:42

    This has been CSS Custom Properties or Variables in 100 Seconds.

  30. 1:46

    Make sure to hit the like button and share this video with your team.

  31. 1:49

    It really helps out these short videos.

  32. 1:50

    Thanks for watching, and I will see you in the next one.