V
V
Vladimir2019-04-04 18:04:18
css
Vladimir, 2019-04-04 18:04:18

What order should I write Sass selectors in?

Now I write selectors in this order:

.block-class {
  @include some-mixin();

  some-rule: value;
  another-rule: value;

  &:hover {
    // ...
  }

  @media ... {
    // ...
  }

  &_modifier {
    // ...
  }

  &__element {
    // ...
  }

  &__another-element {
    // ...
  }
}

But, for example, if at .block:hover(or .block_modifier) you need to change the nested .block__element, then it turns out that this rule is set before the .block__element. Stylelint swears at "no-descending-specificity" with standard settings, and you can get confused.
If you move everything :hoverto &_modifierthe end, then "no-descending-specificity" will be fixed, but it becomes stupidly inconvenient - you need to scroll through the entire file to understand if the element has any states at all.
Yes, I know that it’s not exactly BEM, but sometimes with JS, attaching modifiers to each element on hover is an overkill.
The question is: in what order do you write?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2019-04-04
@notnlwns

.block {
  $this: &;

  color: red;
  
  &__element { font-size: 12px; }

  &:hover &__element { transform: scale(1.1); }

  @media () {}

  //==
  //== Модификация какая-то
  //== ==========================

  #{$this}_mod {
    color: green;
    &__element { font-size: 16px; }
    @media () {}
  }
  #{$this}__mod:hover &__element { transform: scale(1.1); }

  //==
  //== Другая модификация
  //== ==========================

  &_mod { background: yellow; }
  @media () {}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question