Sass Topics
- Sass Introduction
- Sass Installation
- Sass Nested Rules and Properties
- Sass Variables
- Sass @ Rules
- Sass Flow Control
- Sass Operators
- Sass Comments
- Sass Color Functions
- Sass Selector Functions
- Sass Introspection Functions
- Sass Map Functions
- Sass List Functions
- Sass Numeric Functions
- Sass String Functions
Sass selector-extend() Function
Photo Credit to CodeToFun
đ Introduction
The selector-extend()
function in Sass is a powerful feature used to extend one selector with another. This allows you to share styles across multiple selectors without duplicating code.
The selector-extend()
function is particularly useful when you want to apply the styles of one selector to another or when you're working with complex stylesheets and want to keep your code DRY (Don't Repeat Yourself).
đĄ Syntax
The selector-extend()
function takes three arguments:
selector-extend(selector, extendee, extender)
đĸ Parameters
- selector: The original selector to be extended.
- extendee: The target selector whose styles will be extended.
- extender: The selector that will inherit the styles from the extendee.
âŠī¸ Return Value
The function returns a string that represents the new selector with the extended styles applied.
đ Example Usage
Let's explore some examples to understand how selector-extend()
can be used effectively.
đ Example 1: Basic Usage
%base-button {
padding: 10px 20px;
border-radius: 5px;
background-color: #3498db;
color: white;
}
.button-primary {
@extend %base-button;
background-color: #2980b9;
}
.button-secondary {
@extend %base-button;
background-color: #95a5a6;
}
In this example, the %base-button placeholder selector is extended by both .button-primary and .button-secondary. This allows the base button styles to be shared without repeating the code.
đ Example 2: Extending a Nested Selector
.menu {
&-item {
padding: 10px;
border-bottom: 1px solid #ccc;
}
}
.nav-item {
@extend .menu-item;
color: #333;
}
Here, the .nav-item selector extends .menu-item, inheriting its styles while adding its own.
đ Example 3: Using selector-extend() in Complex Scenarios
%common-style {
font-family: Arial, sans-serif;
font-size: 16px;
}
.card {
@extend %common-style;
padding: 20px;
background-color: #f7f7f7;
}
.alert {
@extend %common-style;
padding: 15px;
background-color: #ffdddd;
}
In this example, the %common-style placeholder selector is used to share font styles across both the .card and .alert classes, keeping the code clean and consistent.
đ Conclusion
The selector-extend()
function in Sass is a powerful tool that helps you manage and reuse styles efficiently. By extending selectors, you can avoid code duplication and maintain a cleaner, more maintainable stylesheet. This function is especially useful in large projects where consistency and code organization are crucial.
Mastering selector-extend()
can significantly improve your workflow in Sass, allowing you to create more modular and reusable styles. Experiment with different selectors and combinations to fully leverage the potential of this function in your Sass projects.
đ¨âđģ Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Sass selector-extend() Function), please comment here. I will help you immediately.