Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested classes in SASS

I have the following code:

        li {
            a {
                /* style removed for brevity */
                &:hover {   
                    color:#4f9269;
                    @include background-image(linear-gradient(top, #fff, #f0f6f2));
                }
            }
            &.active a {
                color:#4f9269;
                @include background-image(linear-gradient(top, #fff, #f0f6f2));
            }
        }

When a menu item is active the active class is applied to the li, so I have had to apply the CSS like this.

I have also consiedered writing a mixin:

@mixin activestate() {
    color:#4f9269;
    @include background-image(linear-gradient(top, #fff, #f0f6f2));
}

And then doing this:

        li {
            a {
                /* style removed for brevity */
                &:hover {   
                    activestate();
                }
            }
            &.active a {
                activestate();
            }
        }

Normally with CSS I would write something like this

li a:hover, li.active a {
    /* active styles here */
}

I was wondering if there was a way with SASS to achieve similar output?

like image 206
Ian Jamieson Avatar asked Mar 04 '26 10:03

Ian Jamieson


1 Answers

SCSS can use the same syntax as the CSS. Moreover, CSS is fully compatible with SCSS. So you can use:

li a:hover, li.active a {
   @include activestate();
}

Or in SASS:

li:hover, li.active a 
  +activestate()
like image 184
Mik Avatar answered Mar 06 '26 23:03

Mik