CSS Variables, also known as Custom Properties, allow you to store values that you can reuse throughout your CSS. This makes your CSS more maintainable and easier to read. In this section, we will cover the basics of CSS Variables, how to define and use them, and some practical examples.

What are CSS Variables?

CSS Variables are entities defined by CSS authors that contain specific values to be reused throughout a document. They are particularly useful for themes, design systems, and large projects where consistency and maintainability are crucial.

Key Concepts

  1. Declaration: CSS Variables are declared within a CSS rule that defines their scope.
  2. Usage: CSS Variables are used by referencing their name within the var() function.
  3. Scope: CSS Variables can be scoped globally or locally.

Syntax

Declaring CSS Variables

CSS Variables are declared using the -- prefix followed by the variable name. They are typically defined within the :root selector for global scope.

:root {
    --primary-color: #3498db;
    --secondary-color: #2ecc71;
    --font-size: 16px;
}

Using CSS Variables

CSS Variables are used by referencing their name within the var() function.

body {
    color: var(--primary-color);
    font-size: var(--font-size);
}

h1 {
    color: var(--secondary-color);
}

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Variables Example</title>
    <style>
        :root {
            --primary-color: #3498db;
            --secondary-color: #2ecc71;
            --font-size: 16px;
        }

        body {
            color: var(--primary-color);
            font-size: var(--font-size);
        }

        h1 {
            color: var(--secondary-color);
        }
    </style>
</head>
<body>
    <h1>Welcome to CSS Variables</h1>
    <p>This is an example of using CSS Variables.</p>
</body>
</html>

Practical Examples

Theming with CSS Variables

CSS Variables are particularly useful for creating themes. You can easily switch themes by changing the values of the variables.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Theming with CSS Variables</title>
    <style>
        :root {
            --background-color: #ffffff;
            --text-color: #000000;
        }

        .dark-theme {
            --background-color: #333333;
            --text-color: #ffffff;
        }

        body {
            background-color: var(--background-color);
            color: var(--text-color);
        }
    </style>
</head>
<body>
    <button onclick="toggleTheme()">Toggle Theme</button>
    <h1>Theming with CSS Variables</h1>
    <p>This is an example of theming using CSS Variables.</p>

    <script>
        function toggleTheme() {
            document.body.classList.toggle('dark-theme');
        }
    </script>
</body>
</html>

Responsive Design with CSS Variables

CSS Variables can also be used to create responsive designs by adjusting variable values based on media queries.

:root {
    --font-size: 16px;
}

@media (min-width: 600px) {
    :root {
        --font-size: 18px;
    }
}

@media (min-width: 900px) {
    :root {
        --font-size: 20px;
    }
}

body {
    font-size: var(--font-size);
}

Exercises

Exercise 1: Basic Usage

  1. Define a CSS Variable for a primary color.
  2. Use this variable to set the background color of a div.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exercise 1</title>
    <style>
        :root {
            --primary-color: #ff6347;
        }

        .box {
            background-color: var(--primary-color);
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

Exercise 2: Theming

  1. Create a light and dark theme using CSS Variables.
  2. Add a button to toggle between the themes.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exercise 2</title>
    <style>
        :root {
            --background-color: #ffffff;
            --text-color: #000000;
        }

        .dark-theme {
            --background-color: #333333;
            --text-color: #ffffff;
        }

        body {
            background-color: var(--background-color);
            color: var(--text-color);
        }
    </style>
</head>
<body>
    <button onclick="toggleTheme()">Toggle Theme</button>
    <h1>Theming with CSS Variables</h1>
    <p>This is an example of theming using CSS Variables.</p>

    <script>
        function toggleTheme() {
            document.body.classList.toggle('dark-theme');
        }
    </script>
</body>
</html>

Common Mistakes and Tips

  1. Forgetting the -- Prefix: Always remember to prefix your variable names with --.
  2. Scope Issues: Ensure that your variables are declared in the correct scope. Use :root for global variables.
  3. Fallback Values: Use fallback values in the var() function to handle cases where the variable might not be defined.
color: var(--primary-color, #000000);

Conclusion

CSS Variables are a powerful feature that can greatly enhance the maintainability and flexibility of your CSS. By understanding how to declare and use them, you can create more dynamic and adaptable stylesheets. In the next section, we will explore CSS Functions, which will further expand your CSS capabilities.

CSS Mastery: From Beginner to Advanced

Module 1: Introduction to CSS

Module 2: Text and Font Styling

Module 3: Box Model and Layout

Module 4: Positioning and Floating

Module 5: Flexbox

Module 6: CSS Grid

Module 7: Advanced CSS Techniques

Module 8: Responsive Design

Module 9: Preprocessors and Frameworks

Module 10: Best Practices and Optimization

Module 11: Project: Building a Responsive Website

© Copyright 2024. All rights reserved