I've been reading up on Block-Element-Modifier naming conventions, semantically-named style rules, etc. and I'd like to replace multiple class names in my html with a single, semantic class name. Can't seem to make it happen, though. Example:
I'd like to replace my Bootstrap navbar
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
with
<nav class="header__navbar" role="navigation">
I know I could use jQuery to do this procedurally, but I'd prefer not to. I've tried several approaches using variable names, #{} interpolation syntax, mixins, and @extend, etc. but just get syntax errors or invalid results.
Is this just not something Sass was designed to do?
Sass does support this use case.
What you need is extending.
Have
navbar
,navbar-default
andnavbar-fixed-top
classes defined as you would with a non-semantic approach:Below, extend your
header__navbar
class with those:Once you get rid of non-semantic classes in your HTML markup completely, you no longer need them in your CSS. So turn them into placeholder selectors aka silent selectors. To do so, replace the
.
with%
both in the rules where they are defined and in@extend
directives that use them. E. g.: