Need to rewrite this function using math.div instead of slash. Details mentioned in the URL given below.
https://jsfiddle.net/26ty5aj7
@function px2em($px, $metric: 'em', $base-font-size: 16px) {
@if unitless($px) {
@warn "Assuming #{$px} to be in pixels, attempting to convert it into pixels.";
@return px2em($px * 1px, $metric, $base-font-size);
} @else if unit($px) == em {
@return $px;
}
@return #{($px / $base-font-size) + $metric};
}
// Pixels to rem based on sass-mq
@function px2rem($px) {
@if unit($px) == rem {
@return $px;
}
@return px2em($px, 'rem');
}
@return #{($px / $base-font-size) + $metric};
Unlike other mathematical operations, division in Sass is done with the math. div() function. Although many programming languages use / as a division operator, in CSS / is used as a separator (as in font: 15px/32px or hsl(120 100% 50% / 0.8) ).
LibSass is an implementation of Sass in C/C++, designed to be easy to integrate into many different languages. However, as time wore on it ended up lagging behind Dart Sass in features and CSS compatibility. LibSass is now deprecated—new projects should use Dart Sass instead.
Sass variables are simple: you assign a value to a name that begins with $ , and then you can refer to that name instead of the value itself.
SASS allows for mathematical operations such as addition, subtraction, multiplication and division. You cannot use incompatible units such as px * px or while adding number with px and em leads to produce invalid CSS. Therefore, SASS will display an error if you use invalid units in CSS.
Here are your functions with the updated syntax. Make sure to keep the @use
import at the top of your file.
@use "sass:math";
@function px2em($px, $metric: 'em', $base-font-size: 16px) {
@if unitless($px) {
@warn "Assuming #{$px} to be in pixels, attempting to convert it into pixels.";
@return px2em($px * 1px, $metric, $base-font-size);
} @else if unit($px) == em {
@return $px;
}
$test: #{math.div($px, $base-font-size) + $metric};
@return $test;
}
// Pixels to rem based on sass-mq
@function px2rem($px) {
@if unit($px) == rem {
@return $px;
}
@return px2em($px, 'rem');
}
If you have many instances of division, you may want to take a look at the automatic migration tool provided by Sass, as thoughtfully pointed out by @Andreas in the comments.
# Install the tool globally using npm
npm install -g sass-migrator
# Run the codemod on all .scss files recursively from the working directory
sass-migrator division **/*.scss
# OR
# Use npx to install temporarily and immediately run (similar to above)
npx --yes sass-migrator division **/*.scss
Keep in mind that the deprecation is a transition, and math.div
is currently only supported in Dart Sass (sass
on npm) >=1.33.0 and not supported in Ruby Sass or LibSass (node-sass
on npm).
This means it is safe to update when your Sass files are only compiled in your project. However, maintainers of libraries that export Sass, like Bootstrap for example, should be mindful as it could be a breaking change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With