Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS Sass Basics Improve Your Workflow with Sass Use the Ampersand to Reference Parent Selectors

Amandeep Pasricha
Amandeep Pasricha
14,932 Points

Why does the a:hover need to be nested inside the anchor element?

As asked.

2 Answers

Hello

in SCSS you dont actually have to nest anything. It is perfectly fine to write normal CSS in SCSS.

The reason we nest them is to write less code and make it easier with less typing errors, this might not be apparent straight away on such a small thing as a link but a full navigation for example it will remove the need to write nav out over and over again, here is a small example:

Nested

.header {
    background-color: #f8f9fa;
    //...

    &__logo {
        display: inline-block;
        //...
    }

    &__button {
        cursor: pointer;
        //...
    }

    &__nav {
        display: flex;
        //...

        &-item {
            display: list-item;
            //...
        }

        &-link {
            color: rgba(0,0,0,.9);
            //...
        }
    }
}

Not nested

.header {
    background-color: #f8f9fa;
    //...
}
.header__logo {
    display: inline-block;
    //...
}

.header__button {
    cursor: pointer;
    //...
}

.header__nav {
    display: flex;
    //...
}

.header__nav-item {
    display: list-item;
    //...
}

.header__nav-link {
    color: rgba(0,0,0,.9);
    //...
}

Both of these work in SCSS file but as you can see, i am repeating myself by not nesting, this will potentially lead to so many bugs that can be very hard to debug in CSS.

This is one of SCSS features that make our lives as developers much easier.

I hope this makes more sense now

As for an anchor link, you can follow the same explanation as above and create less code with the folowing:

a {
    /* unvisited link */
    &:link {
        color: #ff0000;
    }

    /* visited link */
    &:visited {
        color: #00ff00;
    }

    /* mouse over link */
    &:hover {
        color: #ff00ff;
    }

    /* selected link */
    &:active {
        color: #0000ff;
    }
}

Now all my anchor pseudo classes are nested, removing the need to type a every time, making my code neater, cleaner and less error prone

Good Luck

tomd
tomd
16,701 Points

You mean like this?

a {
    color: black;

    &:hover {
        color: pink;
    }
}

When your scss compiles down to css it'll look like this.

a {
    color: black;
}

a:hover {
    color: pink
}

The reason you have to nest is because thats just how scss is written.