Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using math.div instead of / in scss

Tags:

sass

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};

like image 275
kumar004 Avatar asked May 25 '21 12:05

kumar004


People also ask

Can you do math in Sass?

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) ).

What is LibSass?

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.

What is the correct way to define a variable in Sass?

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.

What are number operations in Sass?

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.


Video Answer


1 Answers

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');
}

Automated Migration

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

Compatibility

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.

like image 124
evelynhathaway Avatar answered Oct 07 '22 06:10

evelynhathaway