Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass append() Function

Posted in Sass Tutorial
Updated on Nov 23, 2024
By Mari Selvan
👁ī¸ 28 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
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:

Syntax
Copied
Copy To Clipboard
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

example.scss
Copied
Copy To Clipboard
$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

example.scss
Copied
Copy To Clipboard
$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

example.scss
Copied
Copy To Clipboard
$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

example.scss
Copied
Copy To Clipboard
$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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy