Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

    Root scope and Syntax - CSS variables

    Akash GuthaAkash Gutha
    htmlHTML 5

    In this lesson, we will take a look at the syntax of CSS variables. We’ll explore what the root scope is and how is it used, it's syntax. We’ll also look at the basic syntax required to use css variables.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    Instructor: 00:00 I've prepared a document with three paragraphs that contain simple lorem ipsum text. Then, we'll add styles to this document. Let's add in a style to the paragraph text and give it a color of red.

    00:13 As you can see, the change of color has been reflected in our web browser. Now, let's start with our first concept, known as the root scope. Now, you can think about the root scope as global context or global variable.

    00:26 Whatever you put inside these curly braces will be accessible throughout your web page. Now, let's add in our first variable which we'll be calling our primary color and give it a value of red.

    00:38 To use the CSS variable, we write var, and then parentheses, and then inside the parentheses, we write the exact variable declaration name. If you refresh the web page, you can see that the red color is reflected here.

    00:52 Now, let's try changing the value of the primary color to pink. As you can see, the changes are reflected in the web browser. Now, let's try changing it to orange. The changes are still reflected in the web browser.

    01:06 Now, let's try changing the variable's name to have a single dash at the starting. As you can see, the value falls back to black and the primary color is now not affecting the text anymore. That is because the CSS variable standard required you to start every variable with two dashes, or else it's going to fall back to the default value in this case, which is black.