Sass NestingSass Nesting

The next powerful feature is nesting. It allows us to nest one selector within another to create more organized and readable code. We can group styles and reduce the code we need to write.

Let's imagine that we need to create the website navigation.

navigation example

The html for this navigation will look the following:

Considering that besides the navigation, we use the same html tags (ul, li, a) multiple times throughout the project. To add some styles exactly to these elements, we need to use nesting rules in css. So, css can look as follows:

Now, let's see how sass can makes our lives easier. We will group all these styles in one block. The code is rewritten into:

It's like we're nesting the child's style block in its parent block. Pay attention that the & symbol in the &:hover selector is used to reference the parent selector, in this case, a. This is a shorthand way of writing a:hover and makes it easier to write nested selectors without repeating the parent selector multiple times.


What is the benefit of using sass nesting?

Select the correct answer

Everything was clear?

Section 7. Chapter 5