0

I have the following HTML:

<ul id="navbar-main" class="navbar-nav mr-auto">
    <li class="nav-item active">
        <a href="https://travian.dev/materials" class="nav-link nav-materials">
            <span class="invisible">Materials</span>
        </a>
    </li>
</ul>

And the following SCSS:

#navbar-main {
  li {
    &.nav-item {
      .nav-link {
        width: 50px;
        height: 50px;
        background: no-repeat center;
        background-size: contain;

        &.nav-materials {
          background-image: url('../images/buttons/menu-materials.png');
        }
      }

      &.active {
        .nav-link {
          &.nav-materials {
            background: red;
          }
        }
      }
    }
  }
}

This is working, but I'd like it more maintainable/ easier to read. If possible, I'd like it to be like this:

#navbar-main {
  li {
    &.nav-item {
      .nav-link {
        width: 50px;
        height: 50px;
        background: no-repeat center;
        background-size: contain;

        &.nav-materials {
          background-image: url('../images/buttons/menu-materials.png');

          .active { // here something to actually select .nav-item.active .navlink.nav-materials
            background: red;
          }
        }
      }
    }
  }
}
  • You can't, because `.active` is a class on the `
  • ` element. You can't traverse upwards with your selectors.
  • – Terry May 30 '18 at 12:21
  • IMHO this question is about the Sass parent selector `&` (and `@at-root` and `#{&}`) which, well, exists and not the CSS parent selector (this queston was closed as a dupe and reopened) – FelipeAls May 30 '18 at 12:24