borodean / postcss-assets

An asset manager for PostCSS
MIT License
537 stars 32 forks source link

Rem support needed #54

Closed maximgatilin closed 8 years ago

maximgatilin commented 8 years ago

It would be awesome to add rem support to plugin

borodean commented 8 years ago

@gatilin222 what kind of support?

maximgatilin commented 8 years ago

@borodean I want to convert image sizes to rem units, no pixels.

borodean commented 8 years ago

@gatilin222 I'm suspicions that this kind of a feature is far from the scope of Assets while I would like to keep it as simple as possible. One of the ways to resolve this would be using some of the existing Postcss plugins or developing your own one covering this need.

borodean commented 8 years ago

@gatilin222 for instance, this is how I would solve this using postcss-functions:

require('postcss-functions')({
  functions: {
    // Convert pixels to rems
    // eg. for a relational value of 12px write rem(12)
    // Assumes emBase is the font-size of <html>, defaults to 16px
    rem: function (pxValue, emBase) {
      pxValue = parseInt(pxValue, 10);
      emBase = emBase ? parseInt(emBase, 10) : 16;
      return pxValue / emBase + 'rem';
    }
  }
});
.foobar {
  width: rem(width('foobar.png'));
}