DEV Community

Ria Pacheco
Ria Pacheco

Posted on

5 3

Using Sass'/SCSS' %Placeholder Selectors for Same-Sheet Repeatable Styling

Skip ahead

Refresher on Mixins
The Problem
Solution: Placeholder Selectors


Refresher on Mixins

A @mixin is an At Rule feature of SASS/SCSS that allows you to define styles as though they were contained in a global class that can be accessed from other style sheets.

As a basic example, you write them like this (typically in a style sheet called mixins.scss):

// mixins.scss
@mixin guest-avatar {
  width: 35px;
  height: 35px;
  border-radius: 50%;
  border: 2px solid orange;
}
Enter fullscreen mode Exit fullscreen mode

To access them from another location, you import the @mixin style sheet and use @include like so:

// about.component.scss

@import '../scss/mixins.scss';

img {
  @include guest-avatar;
}
Enter fullscreen mode Exit fullscreen mode

The Problem

  • You can't reference a @mixin from within the same style sheet
  • Sometimes you want repeatability so that you can make adjustments to styles at the top of the style sheet only (going beyond basic $variables)

Solution: Placeholder Selectors

With placeholder selectors you can define the same kind of pseudo class with a % in front of the selector:

// some.component.scss

%rounded-top {
  border-radius: 8px 8px 0px 0px;
  padding: 1rem;
  padding-bottom: 3rem;
}
Enter fullscreen mode Exit fullscreen mode

Now you can reference the selector somewhere on the same style sheet below where it was defined:

// some.component.scss

%rounded-top {
  border-radius: 8px 8px 0px 0px;
  padding: 1rem;
  padding-bottom: 3rem;
}

.bottom-sheet {
  @extend %rounded-top;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}
Enter fullscreen mode Exit fullscreen mode

That's it. That's all.

Ria

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video