Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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

.

3 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.

Charles Sok
seal-mask
.a{fill-rule:evenodd;}techdegree
Charles Sok
Front End Web Development Techdegree Student 7,860 Points

Liam how is this better? it seems like it's basically the amount of typing just in a different way. I could ctrl+c the .header and make a copy of it several times to streamline the different types of elements tagged onto the end of it. What am I missing here all of this SASS seems redundant. With a few simple lines of code saved to a clip board it feels like I could save as much if not more time vs learning a new system.