DEV Community

Giulia Chiola
Giulia Chiola

Posted on • Originally published at giuliachiola.dev

2

Sass placeholder and its limits

From the official 📚 Sass documentation

Sass has a special kind of selector known as a “placeholder”. It looks and acts a lot like a class selector, but it starts with a % and it's not included in the CSS output. In fact, any complex selector (the ones between the commas) that even contains a placeholder selector isn't included in the CSS, nor is any style rule whose selectors all contain placeholders.

We can write a placeholder for reusable pieces of code prefixed by the % keyword:

// colors.scss

%colors-per-viewport {
  background: red;

  @media (min-width: 768px) {
    background: blue;
  }
}
Enter fullscreen mode Exit fullscreen mode

and call the placeholder using the syntax @extend %[placeholder-name]

// component.scss

.colors {
  @extend %colors-per-viewport;
}
Enter fullscreen mode Exit fullscreen mode

CSS output:

.colors {
  background: red;
}
@media (min-width: 768px) {
  .colors {
    background: blue;
  }
}
Enter fullscreen mode Exit fullscreen mode

As seen above, we could also declare a code snippet with mediaquery inside.

The small matter

Unfortunately, we cannot call a placeholder declaration inside a mediaquery 😩

For instance, if we try to declare two placeholders and call them inside a media query

%colors-mobile {
  background: yellow;
}

%colors-tablet {
  background: green;
}
Enter fullscreen mode Exit fullscreen mode
.colors-viewport {
  @extend %colors-mobile; // ok!

  @media (min-width: 768px) {
    @extend %colors-tablet; // nope!
  }
}
Enter fullscreen mode Exit fullscreen mode

The code above will throw an error 😭

You may not @extend an outer selector from within @media.
You may only @extend selectors within the same directive.
Enter fullscreen mode Exit fullscreen mode

So, if we really need to reuse a code snipped inside a mediaquery, we can use a mixin declaration.

I know, it is not the correct use of the mixin function... but it's a desperate measure! 😅

@mixin colors-mobile {
  background: yellow;
}

@mixin colors-tablet {
  background: green;
}
Enter fullscreen mode Exit fullscreen mode
.colors-viewport {
  @include colors-mobile; // ok!

  @media (min-width: 768px) {
    @include colors-tablet; // yasss!
  }
}
Enter fullscreen mode Exit fullscreen mode

📚 More info

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (2)

Collapse
 
aileenr profile image
Aileen Rae

I just learned about SASS placeholders today and came here from Google. I'm really glad I found this because I use nested media queries in SASS all the time. Thank you ✨

Collapse
 
giulia_chiola profile image
Giulia Chiola

I'm glad that it has been helpful 😊 Thank you!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay