PostCSS px To em
PostCSS plugin to convert all px
measurements to em
.
.foo {
width: 270px;
margin: 0 15px;
padding: 1em;
border-radius: 10px 16px;
}
@media (min-width: 640px) {
.foo {
width: 100%;
padding: 10px;
}
}
.foo {
width: 16.875em;
margin: 0 0.9375em;
padding: 1em;
border-radius: 10px 1em;
}
@media (min-width: 640px) {
.foo {
width: 100%;
padding: 0.625em;
}
}
Rationale
For Bugherd, we needed to be able to scale our UIs to fit any zoom level on Mobile. To enable this, we change the parents' font-size
and use em
measurements relative to the base font size (usually 16px
) in our components. This PostCSS plugin facilitates this, without requiring us to rewrite all our code to use em
manually.
Usage
Plug it into your PostCSS configuration.
var options = {
base: 16,
};
postcss([require('postcss-px-to-em')(options)])
See PostCSS docs for examples for your environment.