Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass set-nth() Function

Posted in Sass Tutorial
Updated on Sep 30, 2024
By Mari Selvan
👁ī¸ 30 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
Sass set-nth() Function

Photo Credit to CodeToFun

🙋 Introduction

The set-nth() function in Sass is a powerful utility for working with lists. It allows you to replace an item at a specific index in a list with a new value.

This function is particularly useful when you need to update a specific element in a list while preserving the rest of the list's content.

💡 Syntax

The syntax of the set-nth() function is simple and intuitive. It takes three arguments:

Syntax
Copied
Copy To Clipboard
set-nth(list, n, value)

đŸ”ĸ Parameters

  • list: The input list where the modification will occur.
  • n: The index (1-based) of the item you want to replace.
  • value: The new value that will replace the item at the specified index.

↩ī¸ Return Value

The function returns a new list with the item at the specified index replaced by the new value.

📝 Example Usage

Let's explore some examples to understand how the set-nth() function can be applied.

📜 Example 1: Basic Usage

example.scss
Copied
Copy To Clipboard
$original-list: (apple, banana, cherry);
$new-list: set-nth($original-list, 2, orange);

div {
  content: $new-list;
}

In this example, the item at index 2 (which is banana) is replaced by orange. The resulting list is (apple, orange, cherry).

📜 Example 2: Replacing a Value in a List of Colors

example.scss
Copied
Copy To Clipboard
$colors: (red, green, blue);
$updated-colors: set-nth($colors, 3, purple);

body {
  background-color: nth($updated-colors, 3);
}

Here, the third item in the list of colors (blue) is replaced by purple. The background color of the body will be set to purple.

📜 Example 3: Using set-nth() in a Loop

example.scss
Copied
Copy To Clipboard
$fruits: (apple, banana, cherry);

@for $i from 1 through length($fruits) {
  .fruit-#{$i} {
    content: set-nth($fruits, $i, grape);
  }
}

In this example, each iteration of the loop replaces the corresponding item in the $fruits list with grape. The output for each class will show a list with grape at the current index.

🎉 Conclusion

The set-nth() function in Sass is an essential tool for list manipulation. It allows you to precisely modify elements within a list, making it easier to manage and update list-based data structures in your stylesheets. Whether you're adjusting a list of colors, fonts, or any other set of values, set-nth() provides a flexible and efficient way to make targeted changes.

By understanding and using the set-nth() function, you can take full control of list modifications in your Sass projects, leading to more dynamic and maintainable code. Experiment with this function to see how it can streamline your workflow and enhance your ability to manage lists effectively.

👨‍đŸ’ģ 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