Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass Numeric

Sass ceil() Function

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

Photo Credit to CodeToFun

🙋 Introduction

The ceil() function in Sass is used to round numbers up to the nearest integer. This function is particularly useful when working with calculations that require whole numbers, such as layout calculations, grid systems, or any scenario where fractional values need to be rounded up.

💡 Syntax

The syntax for the ceil() function is simple. It takes a single argument:

Syntax
Copied
Copy To Clipboard
ceil(number)

đŸ”ĸ Parameters

  • number: The numeric value you want to round up.

↩ī¸ Return Value

The function returns the smallest integer greater than or equal to the given number.

📝 Example Usage

Here are some examples demonstrating how the ceil() function can be applied in different scenarios:

📜 Example 1: Basic Usage

example.scss
Copied
Copy To Clipboard
$original-value: 4.2;
$rounded-value: ceil($original-value);

.container {
  width: $rounded-value * 50px; // 5 * 50px = 250px
}

In this example, the value 4.2 is rounded up to 5. This can be useful for setting sizes or dimensions in your design.

📜 Example 2: Rounding Negative Numbers

example.scss
Copied
Copy To Clipboard
$negative-value: -3.7;
$rounded-value: ceil($negative-value);

.alert {
  margin-top: $rounded-value * 10px; // -3 * 10px = -30px
}

Here, -3.7 is rounded up to -3. The result is useful when dealing with negative margins or positions.

📜 Example 3: Using ceil() with Calculations

example.scss
Copied
Copy To Clipboard
$total-columns: 10;
$column-width: 100.5px;
$total-width: ceil($total-columns * $column-width); // 101 * 10 = 1010px

.grid {
  width: $total-width;
}

This example calculates the total width of a grid layout. The result is rounded up to ensure the grid width accommodates the columns fully.

🎉 Conclusion

The ceil() function in Sass provides a straightforward way to round numbers up to the nearest integer. This functionality is particularly valuable when precise, whole-number values are needed in design and layout calculations. By using ceil(), you can ensure that your designs are consistent and visually appealing, without dealing with fractional values that may not be practical in certain contexts.

Understanding and applying the ceil() function can simplify your design process and help you achieve cleaner, more accurate results in your Sass projects. Experiment with ceil() to see how it can enhance your layout calculations and overall design precision.

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