Modern Front End Tech logo

modern front end tech

learn css for free

In this chapter, you will learn what CSS nesting is and how to use it effectively to keep your styles organized and readable.

Lesson 4

CSS Nesting

Nesting is a way to write CSS rules in a hierarchical manner, allowing you to group related styles together. This can make your CSS more organized and easier to read.

Core idea

What is nesting?

Nesting is a way to write CSS rules in a hierarchical manner, allowing you to group related styles together. This can make your CSS more organized and easier to read.

Syntax

How to use nesting

To use nesting, you can write your CSS rules inside the parent selector. For example:

.parent {
    color: blue;
    .child {
        color: red;
    }
}

In this example, the .child selector is nested inside the .parent selector, meaning that the styles for .child will only apply when it is inside an element with the class .parent.

Benefits

Benefits of nesting

Nesting can help you keep your CSS organized and reduce repetition. It also makes it easier to understand the relationship between different elements in your HTML structure.

Example

Nesting with pseudo-classes

You can also nest pseudo-classes like :hover inside a parent selector to keep interactive styles close to the base styles.

.button {
    background-color: blue;
    &:hover {
        background-color: darkblue;
    }
}
Checkpoint

Key Nesting Terms to Know

our youtube channel