YouTip LogoYouTip

Sass Functions

## Sass Functions: A Comprehensive Reference Sass (Syntactically Awesome Style Sheets) provides a rich set of built-in functions that can be called directly within your CSS rules. These functions allow you to manipulate colors, strings, numbers, lists, maps, and selectors dynamically, making your stylesheets more modular, reusable, and maintainable. --- ## Introduction to Sass Functions Sass functions process inputs and return a single value. Unlike mixins (which output blocks of CSS styles), functions are designed to perform calculations, format data, or transform values. Sass categorizes its built-in functions into several specialized modules based on the data types they operate on. --- ## Built-in Function Categories Below is an overview of the core Sass function categories. You can use these functions to perform complex operations directly in your stylesheets. | No. | Function Category | Description | | :--- | :--- | :--- | | 1 | **String Functions** | Used to manipulate, format, and inspect text strings (e.g., changing case, finding substrings, or quoting/unquoting). | | 2 | **Numeric Functions** | Used for mathematical operations, rounding numbers, finding minimum/maximum values, and handling units. | | 3 | **List Functions** | Used to access, modify, combine, and inspect Sass lists (arrays of values). | | 4 | **Map Functions** | Used to retrieve, merge, and manipulate key-value pairs in Sass maps. | | 5 | **Selector Functions** | Used to inspect, decompose, and dynamically construct CSS selectors. | | 6 | **Introspection Functions** | Used to inspect the state of your stylesheet (e.g., checking if a variable, mixin, or function exists, or determining a value's data type). | | 7 | **Color Functions** | Used to adjust, mix, generate, and analyze colors (e.g., adjusting opacity, lightness, saturation, or converting color spaces). | --- ## Syntax and Usage ### Calling Built-in Functions In modern Sass (Dart Sass), built-in functions are organized into modules. To use them, you import the module using the `@use` rule and call the function using the namespace. ```scss // Import the math module @use "sass:math"; .container { // Use the percentage function from the math module width: math.percentage(0.75); // Compiles to: width: 75%; } ``` *Note: While global functions (like `percentage(0.75)`) are still supported for backwards compatibility, using the module system (e.g., `math.percentage()`) is the modern, recommended best practice.* ### Custom Functions In addition to built-in functions, Sass allows you to define your own custom functions using the `@function` directive. ```scss @function calculate-rem($pixel-size, $base-size: 16px) { @return ($pixel-size / $base-size) * 1rem; } .sidebar { // Call the custom function font-size: calculate-rem(24px); // Compiles to: font-size: 1.5rem; } ``` --- ## Code Examples Here are practical examples demonstrating how different categories of Sass functions are used in real-world styling. ### 1. Color Manipulation ```scss @use "sass:color"; $primary-color: #3498db; .button { background-color: $primary-color; &:hover { // Darken the primary color by 10% background-color: color.adjust($primary-color, $lightness: -10%); } &:active { // Desaturate the primary color by 20% background-color: color.adjust($primary-color, $saturation: -20%); } } ``` ### 2. Map and List Operations ```scss @use "sass:map"; $theme-colors: ( "primary": #007bff, "secondary": #6c757d, "success": #28a745 ); .alert-success { // Retrieve a value from a map using map.get() background-color: map.get($theme-colors, "success"); } ``` --- ## Considerations and Best Practices 1. **Prefer Modules over Global Functions**: Use `@use "sass:math"` instead of global functions like `round()`. This prevents namespace collisions and prepares your codebase for future Sass updates. 2. **Functions vs. Mixins**: Use **functions** (`@function`) when you need to compute and return a value. Use **mixins** (`@mixin`) when you want to output blocks of CSS declarations. 3. **Unit Safety**: When writing custom mathematical functions, ensure your inputs have compatible units (e.g., do not try to add `px` to `em` directly without conversion). 4. **Performance**: Avoid overly complex recursive functions in large codebases, as they can increase compilation times. Use built-in Sass functions whenever possible, as they are highly optimized.
← Ng Ng HrefNg Ng Dblclick β†’