Den rem
font-size enhet liknar em
, bara i stället för cascading det är alltid i förhållande till roten (html) element (mer information). Detta har ganska bra modernt webbläsarstöd, det är bara IE 8 och nedåt som vi behöver ge px
fallbackar för.
Istället för att upprepa oss överallt kan vi använda en MINDRE eller SASS mixins för att hålla den ren. Dessa mixins förutsätter:
html ( font-size: 62.5%; /* Sets up the Base 10 stuff */ )
.font-size(@sizeValue) ( @remValue: @sizeValue; @pxValue: (@sizeValue * 10); font-size: ~"@(pxValue)px"; font-size: ~"@(remValue)rem"; )
@mixin font-size($sizeValue: 1.6) ( font-size: ($sizeValue * 10) + px; font-size: $sizeValue + rem; )
Användande
p ( .font-size(13); )
p ( @include font-size(13); )
(Tack Gabe Luethje)
En annan SCSS med en annan inställning av Karl Merkli:
@function strip-unit($num) ( @return $num / ($num * 0 + 1); ) @mixin rem-fallback($property, $values… ) ( $max: length($values); $pxValues: ''; $remValues: ''; @for $i from 1 through $max ( $value: strip-unit(nth($values, $i)); $pxValues: #($pxValues + $value*16)px; @if $i < $max ( $pxValues: #($pxValues + " "); ) ) @for $i from 1 through $max ( $value: strip-unit(nth($values, $i)); $remValues: #($remValues + $value)rem; @if $i < $max ( $remValues: #($remValues + " "); ) ) #($property): $pxValues; #($property): $remValues; )
Så du kan göra:
@include rem-fallback(margin, 10, 20, 30, 40);
och få:
body ( margin: 160px 320px 480px 640px; margin: 10rem 20rem 30rem 40rem; )