78

Having multiple two-class selectors for a single declaration block, is it possible to simplify the following (i.e. not having to repeat the body tag):

body.shop, body.contact, body.about, body.faq {background-color:#fff;}
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232

6 Answers6

128

try this:

body{
   &.shop, &.contact, &.about, &.faq {
        background-color:#fff;
    }
}
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
29

In this case we can use @each directive:

$pages: shop, contact, about, faq;

body {
  @each $page in $pages {
    &.#{$page} {
      background-color:#FFF;
    }
  }
}

sassmeister.com

Dmitry
  • 544
  • 1
  • 5
  • 12
8
body {
    &.shop, &.contact {
        // Styles here...
    }
}
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
1

If you are using sass compiled by the node, that may do.

    body {
      .shop, .contact, .about, .faq {
          background-color:#FFFFFF;
      }
    }
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
Alec
  • 19
  • 1
0

I am not sure that this directly addresses the question, but with scss you can reuse two classes in one like this:

.displayFlex {
  display: '-webkit-box';      /* OLD - iOS 6-, Safari 3.1-6 */
  display: '-moz-box';         /* OLD - Firefox 19- (buggy but mostly works) */
  display: '-ms-flexbox';      /* TWEENER - IE 10 */
  display: '-webkit-flex';     /* NEW - Chrome */
  display: flex;             /* NEW, Spec - Opera 12.1, Firefox 20+ */

}

.flexOne {
  flex: 1; /* NEW, Spec - Opera 12.1, Firefox 20+ */
  -webkit-box-flex: 1; /* OLD - iOS 6-, Safari 3.1-6 */
  -moz-box-flex: 1; /* OLD - Firefox 19- */
  -webkit-flex: 1; /* Chrome */
  -ms-flex: 1; /* IE 10 */
}

.boxWrapper01 {
  @extend .displayFlex;
  @extend .flexOne;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  background-color: beige;
}
Roman
  • 19,236
  • 15
  • 93
  • 97
-1

Parent child relationship in sass

parent_tag {
    .child {
       // rules here
    }
}
Naveen Vignesh
  • 1,351
  • 10
  • 21