SCSS extension and placeholders
🎯 Introduction
This article shows you how to use SCSS extension and placeholders to write concise, efficient styles. Combined with BEM naming and sensible file organization, these features create a clear structure and improve reuse and maintainability. They are valuable skills for anyone getting started with CSS and SCSS.
🌟 Using @extend
SCSS uses @extend to let one selector share the styles of another, avoiding repeated declarations.
Suppose you have a set of base button styles:
.button {
padding: 10px 20px;
border-radius: 4px;
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
.button:hover {
background-color: #2980b9;
}
Now you want .button-primary to reuse .button while changing its colors. Use @extend:
.button-primary {
@extend .button;
background-color: #e74c3c;
&:hover {
background-color: #c0392b;
}
}
.button,
.button-primary {
padding: 10px 20px;
border-radius: 4px;
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
.button:hover,
.button-primary:hover {
background-color: #2980b9;
}
.button-primary {
background-color: #e74c3c;
}
.button-primary:hover {
background-color: #c0392b;
}
.button-primary now shares all the styles of .button without duplicating the declarations.
@extend adds the extending selector to the relevant selector groups, sharing styles without redundant declarations. Avoid extending too many complex selectors, which can produce complicated CSS rules.When should I use @extend?
- When multiple classes share base styles that other classes can extend
- When reusing consistent styles across buttons, cards, and form elements
- When keeping common styles in one place simplifies maintenance
A short example
.message {
padding: 10px;
border-radius: 3px;
color: #fff;
}
.message-success {
@extend .message;
background-color: #2ecc71;
}
.message-error {
@extend .message;
background-color: #e74c3c;
}
.message,
.message-error,
.message-success {
padding: 10px;
border-radius: 3px;
color: #fff;
}
.message-success {
background-color: #2ecc71;
}
.message-error {
background-color: #e74c3c;
}
This example uses extension to create several message styles with concise, readable code.
🌟 Using %placeholder
%placeholder defines a placeholder selector in SCSS. Unlike a regular class selector, it is not emitted as a CSS selector itself and is used through @extend. This provides another way to reuse styles.
For example:
%btn-base {
padding: 10px 15px;
border-radius: 5px;
display: inline-block;
text-align: center;
cursor: pointer;
}
.btn-primary {
@extend %btn-base;
background-color: #3498db;
color: white;
}
.btn-secondary {
@extend %btn-base;
background-color: #95a5a6;
color: white;
}
.btn-secondary,
.btn-primary {
padding: 10px 15px;
border-radius: 5px;
display: inline-block;
text-align: center;
cursor: pointer;
}
.btn-primary {
background-color: #3498db;
color: white;
}
.btn-secondary {
background-color: #95a5a6;
color: white;
}
Only .btn-primary and .btn-secondary appear in the generated CSS, sharing the styles defined in %btn-base.
Why use %placeholder instead of a regular class?
- Avoid generating unnecessary CSS
- Make it explicit that styles are emitted only when extended
- Define abstract base styles conveniently
%placeholder for base styles to avoid emitting unused class selectors and keep stylesheets smaller. It provides a more modular approach than creating general-purpose classes solely for reuse.🌟 Combining BEM with SCSS
BEM (Block Element Modifier) is a naming convention for structured, maintainable CSS.
BEM names typically follow this pattern:
.block {}
.block__element {}
.block--modifier {}
SCSS nesting makes BEM styles easier to express and follow.
For example:
.card {
background: #f4f4f4;
border-radius: 6px;
padding: 20px;
&__title {
font-size: 20px;
font-weight: bold;
}
&__content {
margin-top: 10px;
font-size: 14px;
}
&--highlighted {
border: 2px solid #3498db;
}
}
.card {
background: #f4f4f4;
border-radius: 6px;
padding: 20px;
}
.card__title {
font-size: 20px;
font-weight: bold;
}
.card__content {
margin-top: 10px;
font-size: 14px;
}
.card--highlighted {
border: 2px solid #3498db;
}
.card__title and .card__content are elements of .card, while .card--highlighted is a modifier of .card.
.block and keeps the style structure clear and maintainable.Tips for organizing BEM + SCSS
- Use the block as the outermost selector
- Nest elements using
&__xxx - Nest modifiers using
&--xxx - Keep each block in its own SCSS file or module
❓ Questions and answers
Q1: What is the difference between @extend and %placeholder?
@extend is the mechanism for sharing styles by extending a selector. %placeholder defines a selector that is never emitted on its own and must be extended, helping avoid unused CSS.
Q2: How does BEM benefit from SCSS nesting?
It reduces repeated long class names and makes the structure easier to follow, maintain, and work on as a team.
Q3: How should I split SCSS files?
Keep foundational styles and variables in base, group components into modules, put layout styles in layout, and give page styles separate files. Load the modules from a main entry point.
🧪 Exercises
Exercise 1: Style a button group with @extend and %placeholder
Create a %btn-base placeholder with base button styles. Extend it in .btn-primary and .btn-danger, giving each its own background color and hover effect.
%btn-base {
/* Add base button styles here */
}
.btn-primary {
/* Extend %btn-base and set the primary button color */
}
.btn-danger {
/* Extend %btn-base and set the danger button color */
}
Exercise 2: Build a card with BEM and SCSS nesting
Create a .card block with .card__header and .card__body elements, plus a .card--featured modifier that changes the border color.
.card {
/* Base card styles */
&__header {
/* Header styles */
}
&__body {
/* Body styles */
}
&--featured {
/* Featured card modifier */
}
}
🎉 You now know how to use @extend, %placeholder, BEM with SCSS, and modular file organization! Put these techniques into practice to keep your styles clearer and easier to maintain!
