Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Root scope and Syntax - CSS variables

    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 Pro 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
    orLog In




    Instructor: 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.

    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.

    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.

    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.

    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.

    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.