Advanced CSS: Variables, Nesting, & Imports

Learning Goals

  • Understand the benefits of using variables in CSS
  • Master the concept of nesting in CSS
  • Learn how to use imports to modularize CSS files


In this lesson, we will delve into advanced CSS techniques that go beyond basic styling. These techniques include the use of variables, nesting, and imports, which are essential for maintaining scalable and organized CSS codebases.

Variables in CSS

CSS variables, also known as custom properties, allow you to store and reuse values throughout your stylesheets. This promotes consistency and makes it easier to update styles across your project.

Variable Declaration Syntax

:root {
  --primary-color: #007bff;
  --secondary-color: #6c757d;

.subtitle {
  color: var(--primary-color);
  background-color: var(--secondary-color);

Example Usage

<h1 class="subtitle">Hello, CSS Variables!</div>

In this example, we define primary and secondary colors as CSS variables and apply them to an HTML element. This way, if we need to change the color scheme of our website, we only need to update the variable values in one place.

Nesting in CSS

Nesting allows you to define styles for nested elements in a more intuitive and readable way. This is particularly useful when working with complex HTML structures.

Nesting Syntax

.navbar {
  background-color: #333;
  ul {
    list-style-type: none;
    li {
      display: inline-block;
      a {
        color: #fff;
        text-decoration: none;

Example Usage

<nav class="navbar">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>

In this example, we define styles for a navigation bar and its nested list items and links. The nesting structure mirrors the HTML structure, making it easier to understand and maintain the CSS code.

Use nesting with CAUTION

Be aware that having too much nesting can be a problem - resulting in hard to maintain CSS that is overly specific (remember specificity?). Try to avoid excessive levels of nesting unless absolutely necessary.

Imports for Modularization

CSS imports allow you to split your stylesheets into smaller, modular files, making your codebase more maintainable and organized.

Import Syntax

@import 'reset.css';
@import 'variables.css';
@import 'layout.css';
@import 'components/buttons.css';
/* Additional imports */

By importing separate CSS files, you can compartmentalize styles for different components or sections of your website, improving code readability and facilitating collaboration.


In this lesson, we explored advanced CSS techniques including the use of variables, nesting, and imports. These techniques empower developers to write more maintainable, scalable, and organized CSS code. By mastering these concepts, you’ll be better equipped to tackle complex styling challenges and build more efficient web applications.

Now, let’s put your knowledge into practice with some exercises.


Let’s get some practice with these concepts working with a static e-commerce website . Clone down this repo following the setup instructions and work through the iterations in the instructions to refactor the site using what you’ve learned!

Checks For Understanding

  • What is the purpose of using variables in CSS?
  • Why is nesting useful for organizing CSS code?
  • Explain the benefits of modularizing stylesheets using imports.

Further Resources

Lesson Search Results

Showing top 10 results