This solves the problem of incompatible with the low version of IE, but gives birth to another disadvantage, which is to increase the amount of code. Must have the fish and bear paw many times can not be both.
In Sassrem
The useIn CSS, implementations px
and rem
transformations are very simple, but each use needs to be computed. Although in the html
setting font-size:62.5%;
will bring convenience, but eventually some annoying, not long-term. Now that we have studied sass, we should think about how to get sass to help us with these calculations. Next, we'll show you how to use the SASS implementation px
and rem
calculation.
rem
In@function
In the use ofThe sass can also be used as a em
px
conversion to rem
. This process is also accomplished by Sass @function
.
Depending on rem
the principle of usage, you can know that px
rem
a turn needs to html
set a value in the root element font-size
, because rem
it is relative to the html
root element. To define a function to px
go in Sass rem
, first set a default variable:
$browser the value of the-DEFAULT-FONT-SIZE:16PX!default;//variable can be defined according to your needs
and the declaration that needs to be displayed in the html
root element font-size
:
$browser the value of the-DEFAULT-FONT-SIZE:16PX!default;//variable can be defined according to your needs
Then we @function
can achieve px
the rem
calculation by:
@function pxTorem($px){//$px为需要转换的字号 @return $px / $browser-default-font-size * 1rem;}
Once defined @function
, the actual use is much simpler:
{ font-size: $browser-default-font-size;} { font-size: pxtorem (12px);} { font-size: 16px;} { font-size: 0.75rem;}
However, the defined function pxTorem()
px
rem
does not serve the calculation of multiple property values at the same time, although it implements the converted calculation:
{ font-size: pxtorem (12px); margin: pxtorem (5px 10px); //Simultaneous calculation of multiple values will be an error }
If you use this, you will get an error when compiling:
/Users/airen/Sites/testSass/style.scss error style.scss (Line 4: Undefined operation: "5px 10px/16px times 1rem".)
This means that if the style needs to set multiple property values at the same time, it pxTorem()
becomes too restrictive, in other words, the function is only suitable for use on attributes with a single property value, for example font-size
. If you want to use it forcibly, you can use more than one pxTorem()
:
{ font-size: pxtorem (12px); margin: pxtorem (5px) Pxtorem (10px) Pxtorem (15px); Border: pxtorem (1px) solid #f36;} { font-size: 0.75rem; Margin: 0.3125rem 0.625rem 0.9375rem; Border: 0.0625rem solid #ff3366;}
In Sassmixin
Realizerem
In addition to using @function
px
the implementation transformation rem
, you can also use the implementation of the sass in the mixin
px
rem
.
font-size
is one of the most common attributes in a style, let's take a look at a simple mixin
, font-size
go-to implementation px
rem
:
@mixin font-size ($target) { font-size: $target; font-size: ($target/$browser-default-font-size) * 1rem;}
In practice, it can be @include
defined by invoking @mixin font-size
:
{ @include font-size (12px);} { font-size: 12px; Font-size: 0.75rem; }
In fact, this mixin
is too weak to achieve the effect we need, because many of our style attributes don't have a single attribute. In order for multiple properties to be set to multiple values, it is necessary to mixin
make a function extension:
@mixin Remcalc ($property, $values ...){$max:Length ($values);//Returns the length value of the $values list $pxValues:"';$remValues:"';@for $i from 1 through $max {$value:strip-units (Nth ($values, $i));//Returns the $i value in the $values list and removes the unit value $browser-default-font-size:strip-units ($browser-default-font-size);$pxValues:#{$pxValues + $value * $browser-default-font-size}px; @if $i < $max{$pxValues:#{$pxValues + ""}; }} @for $i from 1 through $max{$value:strip-units (Nth ($values, $i));$remValues:#{$remValues + $value}REM; @if $i < $max{$remValues:#{$remValues + ""}; } } #{$property}: $pxValues; #{$property}: $remValues;}
In this, remCalc()
two parameters are defined $property
and $values...
. $property
It represents a style attribute and $values...
represents one or more property values.
Note: The following custom functions are used in the definitions above remCalc
strip-units
, mainly for removing units, see Sass Basic--px to EM mixin and @function for a strip-units
function definition method.
px
rem
once the definition of a turn is mixin
complete, you can @include
refer to it by:
{ @include remcalc (width,45); @include Remcalc (margin,1,.5,2,3);} { width: 720px; Width: 45rem; margin: 16px 8px 32px 48px; margin: 1rem 0.5rem 2rem 3rem;}
It is important to take a value in the actual use of the value. remCalc()
$values
rem
Conversion of SASS base--rem to px