Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include the parent properties in an extended Sass class?

Tags:

I'd like to implement something like the BEM model in my Sass library. But I'm struggling to find a clean way to do this.

For example, I'd like to declare a 'base' style for a common element, and then extend it with useful variations:

.container {
  margin: 10%;
  background: #eee;

  &-featured {
    border: 2px solid #999;
  }

}

The problem here is that the generated .container-featured class only contains the border property—Sass doesn't include the margin and background from its 'parent' class.

So you end up having to double up on classes in your markup to get the desired results:

<div class="container container-featured">
  ...
</div>

Is there some way to pull the properties from a parent class down into that modifier class, so you can get the same visual result just referencing the modifier class in your markup?

<div class="container-featured">
  <!-- has margin, background, and border styles via just modifier class -->
</div>

I've tried using mixins to do this, but things get verbose and repetitive very quickly:

@mixins container {
  margin: 10%;
  background: #eee;
}

.container {
  @include container;

  &-featured {
    @include container;
    border: 2px solid #999;
  }

}

Is there a simple, clean way of achieving this with Sass?

like image 636
markedup Avatar asked Oct 21 '14 09:10

markedup


2 Answers

What you are looking for is the @extend directive. @extend allows you share a set of CSS properties from one selector to another. This means that you would only need to use the container-featured class.

Example

.container {
  margin: 10%;
  background: #eee;

  &-featured {
    @extend .container;
    border: 2px solid #999;
  }
}

compiles to:

.container,
.container-featured {
    margin: 10%;
    background: #eee;
}

.container-featured {
    border: 2px solid #999;
}
like image 96
Colin Bacon Avatar answered Oct 13 '22 20:10

Colin Bacon


You should use mixin in BEM not in Sass!

Mixins are just that - usage of several blocks and/or elements on the same DOM node.

A single DOM node can represent:

  • several blocks b-menu b-head-menu
  • a block and an element of the same block b-menu b-menu__layout
  • a block and an element of another block b-link b-menu__link
  • elements of different blocks b-menu__item b-head-menu__item
  • a block with a modifier and another block b-menu b-menu_layout_horiz b-head-menu
  • several different blocks with modifiers b-menu b-menu_layout_horiz b-head-toolbar b-head-toolbar_theme_black

Read more at: http://bem.github.io/bem-method/html/all.en.html, section Mixin.

Also you can use i-blocks (abstract blocks), so your .container will be .i-container, read more: http://bem.github.io/bem-method/html/all.en.html, section Naming conventions.

And with Sass you can implement i-block as

<div class="container-featured">
  ...
</div>

%i-container {
  // abstract block styles
  margin: 10%;
  background: #eee;
}

.container-featured {
  @extend %i-container;
  border: 2px solid #999;
}

Without Sass, mixin in the BEM are made as follows:

<div class="i-container container-featured">
  ...
</div>

.i-container {
  // abstract block styles
  margin: 10%;
  background: #eee;
}

.container-featured {
  border: 2px solid #999;
}
like image 43
Ihor Zenich Avatar answered Oct 13 '22 20:10

Ihor Zenich