Multiple two-class selectors in Sass
Asked Answered
S

6

79

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;}
Sacrosanct answered 1/9, 2013 at 18:32 Comment(0)
B
129

try this:

body{
   &.shop, &.contact, &.about, &.faq {
        background-color:#fff;
    }
}
Burin answered 1/9, 2013 at 18:33 Comment(0)
W
30

In this case we can use @each directive:

$pages: shop, contact, about, faq;

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

sassmeister.com

Wryneck answered 3/2, 2017 at 9:48 Comment(0)
D
8
body {
    &.shop, &.contact {
        // Styles here...
    }
}
Detour answered 1/9, 2013 at 18:33 Comment(0)
B
1

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

    body {
      .shop, .contact, .about, .faq {
          background-color:#FFFFFF;
      }
    }
Beery answered 6/12, 2020 at 3:19 Comment(0)
M
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;
}
Moorefield answered 11/6, 2023 at 20:43 Comment(0)
G
-1

Parent child relationship in sass

parent_tag {
    .child {
       // rules here
    }
}
Graffito answered 29/9, 2021 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.