Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass unit() Function

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

Photo Credit to CodeToFun

🙋 Introduction

The unit() function in Sass is a useful tool for managing and extracting the unit of a value. It allows you to retrieve the unit from a value, such as px, %, em, or any other CSS unit, which can be particularly helpful in dynamic calculations and unit-based operations.

Understanding and utilizing the unit() function can enhance your ability to create flexible and responsive designs in Sass.

💡 Syntax

The syntax of the unit() function is simple and consists of a single argument:

Syntax
Copied
Copy To Clipboard
unit(value)

đŸ”ĸ Parameters

  • value: The input value from which you want to extract the unit (e.g., 10px, 50%, 2em).

↩ī¸ Return Value

The function returns the unit of the given value as a string. If the value does not have a unit, the function returns null.

📝 Example Usage

Let's explore some examples to understand how the unit() function works in various scenarios.

📜 Example 1: Extracting the Unit from a Value

example.scss
Copied
Copy To Clipboard
$padding: 15px;
$padding-unit: unit($padding);

body {
  padding: $padding;
  // This will output: padding: 15px;
  // The unit is extracted as 'px'
}

In this example, the unit() function extracts px from the 15px value.

📜 Example 2: Using unit() in Calculations

example.scss
Copied
Copy To Clipboard
$base-width: 100%; 
$margin: 20px;

$margin-unit: unit($margin);

.container {
  width: $base-width;
  margin-left: $margin;
  // Here, $margin-unit will be 'px', which can be useful if you need to apply margin with the same unit elsewhere.
}

Here, unit() helps in identifying the unit of 20px, which can be useful if you need to perform further calculations or adjustments.

📜 Example 3: Handling Values Without Units

example.scss
Copied
Copy To Clipboard
$border-radius: 10; // No unit

$border-radius-unit: unit($border-radius);

.box {
  border-radius: $border-radius;
  // The unit is null, indicating that $border-radius does not have a unit
}

In this case, since 10 has no unit, unit() returns null.

🎉 Conclusion

The unit() function in Sass is a valuable tool for extracting and working with units in your stylesheets. It helps you manage unit-based values more effectively, ensuring consistency and flexibility in your designs. Whether you are performing calculations or need to adapt values dynamically, unit() provides the functionality to handle units with ease.

By leveraging the unit() function, you can create more adaptable and responsive styles, improving the overall efficiency and effectiveness of your CSS code. Experiment with unit() to better understand how it can fit into your Sass workflow and enhance your design process.

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