Sass Numeric
Sass append() Function
Photo Credit to CodeToFun
đ Introduction
The append()
function in Sass is used to add a value to the end of a list. Lists are a crucial part of Sass, allowing you to manage collections of values, such as colors, dimensions, or other CSS properties.
The append()
function simplifies the process of adding new items to these lists, making it easier to manage and manipulate complex stylesheets.
đĄ Syntax
The append()
function in Sass takes two or three arguments:
append(list, value, separator)
đĸ Parameters
- list: A list of values (e.g., (1px, 2px, 3px) or 1px 2px 3px).
- value: The value you want to add to the end of the list.
- separator (Optional): Defines how the list items are separated (comma or space).
âŠī¸ Return Value
The append()
function returns a new list with the specified value added to the end. If a separator is specified, the function uses that separator in the returned list.
đ Example Usage
Here are some examples that demonstrate how to use the append()
function in different scenarios.
đ Example 1: Basic Usage
$list: 10px 20px 30px;
$new-list: append($list, 40px);
.container {
padding: $new-list; // Outputs: 10px 20px 30px 40px
}
In this example, the value 40px is appended to the list 10px 20px 30px, resulting in the list 10px 20px 30px 40px.
đ Example 2: Appending with a Comma Separator
$list: (red, green, blue);
$new-list: append($list, yellow, comma);
body {
background-image: linear-gradient($new-list);
// Outputs: linear-gradient(red, green, blue, yellow)
}
Here, the color yellow is appended to the list (red, green, blue) with a comma separator, producing the list (red, green, blue, yellow).
đ Example 3: Appending to an Empty List
$list: ();
$new-list: append($list, 'first-item');
.header {
content: $new-list; // Outputs: 'first-item'
}
This example shows how to append a value to an initially empty list, resulting in a list containing just the new value.
đ Example 4: Using a Custom Separator
$list: 1 2 3;
$new-list: append($list, 4, comma);
footer {
margin: $new-list; // Outputs: 1, 2, 3, 4
}
In this case, the list 1 2 3 (which originally uses spaces as separators) is converted to a comma-separated list when 4 is appended, resulting in 1, 2, 3, 4.
đ Conclusion
The append()
function in Sass is an essential tool for list manipulation. Whether you are working with colors, dimensions, or other types of data, append()
makes it easy to dynamically build and manage lists. By understanding how to use this function effectively, you can write more modular and maintainable stylesheets.
Experimenting with different values and separators in append()
can help you unlock new possibilities in your Sass projects, enabling you to create more flexible and powerful CSS code.
đ¨âđģ 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 append() Function), please comment here. I will help you immediately.