postcss-rem
PostCSS plugin to use rem units with optional pixel fallback. Based on sass-rem.
Breaking change in 3.0: changed default function name to rem-convert
as CSS now use rem()
for calculating the remainder.
See also: startijenn-rem, vanilla JavaScript version.
Example
Input
.demo {
font-size: rem-convert(24px);
padding: rem-convert(5px 10px);
margin: rem-convert(10px 0.5rem);
border-bottom: rem-convert(1px solid black);
box-shadow: rem-convert(
0 0 2px #ccc,
inset 0 0 5px #eee
);
text-shadow: rem-convert(1px 1px) #eee, rem-convert(-1px) 0 #eee;
}
Output
.demo {
font-size: 1.5rem;
padding: 0.3125rem 0.625rem;
margin: 0.625rem 0.5rem;
border-bottom: 0.0625rem solid black;
box-shadow: 0 0 0.125rem #ccc, inset 0 0 0.3125rem #eee;
text-shadow: 0.0625rem 0.0625rem #eee, -0.0625rem 0 #eee;
}
Options
With baseline
to 10
(html { font-size: 62.5%; }
) and fallback
to true
:
.demo {
font-size: 24px;
font-size: 2.4rem;
padding: 5px 10px;
padding: 0.5rem 1rem;
margin: 10px 5px;
margin: 1rem 0.5rem;
border-bottom: 1px solid black;
border-bottom: 0.1rem solid black;
box-shadow: 0 0 2px #ccc, inset 0 0 5px #eee;
box-shadow: 0 0 0.2rem #ccc, inset 0 0 0.5rem #eee;
text-shadow: 1px 1px #eee, -1px 0 #eee;
text-shadow: 0.1rem 0.1rem #eee, -0.1rem 0 #eee;
}
With convert
to px
(for a lt-ie9 only stylesheet for example):
.demo {
font-size: 24px;
padding: 5px 10px;
margin: 10px 8px;
border-bottom: 1px solid black;
box-shadow: 0 0 2px #ccc, inset 0 0 5px #eee;
text-shadow: 1px 1px #eee, -1px 0 #eee;
}
Usage
Install with npm i postcss-rem
and use with PostCSS:
postcss([require("postcss-rem")]);
Example with custom options:
postcss([
require("postcss-rem")({
name: "convert-rem",
baseline: 10,
fallback: true,
precision: 6,
}),
]);
See PostCSS docs for examples for your environment.