C
C
colorkid2018-06-19 11:16:48
css
colorkid, 2018-06-19 11:16:48

How to inherit nesting in SCSS(SASS)?

Hello. Periodically there are such pieces of code:

.main-menu {
        display: none;
        position: absolute;
        left: 0;
        top: 50px;
        background-color: #fff;

        &--open {
            display: block;

            .main-menu__link {
            }
        }
    }

how not to write main-menu__link in this case completely, but as always through &, but taking into account the fact that we have a parent block modifier? To get something like this:
.main-menu {
        display: none;
        position: absolute;
        left: 0;
        top: 50px;
        background-color: #fff;

        &--open {
            display: block;

           &__link{} // и что бы получалось в итоге селектор main-menu__link а не main-menu--open__link
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2018-06-19
@colorkid

.main-menu {
    $this: &;
    display: none;
    &--open {
        display: block;
        #{$this}__link {  }
    }
}

But I prefer this one:
.main-menu {
    display: none;
    &--open { display: block; }
    &--open &__link {  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question