Stylus PX to REM Mixin

By  on  

The REM unit is the hottest thing since Christina Ricci ... it's hot.  And it's especially hot when it comes to font size.  Rather than paraphrase a bunch of documentation as to why REM is important, I'd like to point you to Jonathan Snook's outstanding Font sizing with rem post, where he explains sizing with PX, EM, and how REM comes into the picture.  Here's how I implemented REM sizing within Stylus with a PX fallback!

The Stylus CSS

My method uses two mixins to accomplish the feat: one to set the base font-size and another to rem-ify the pixel unit:

set-font-size(value) {
    font-size: value; /* add PX (or original value) as backup */

    if (value is inherit) {
        /* do nothing, "inherit" has no unit */
    } else {
        font-size: remify(value);

remify(value) { /* separate mixin, so it can be used elsewhere */
    u = unit(value);

    if (u is 'px') {
        return unit(value/16, 'rem');
    } else {
        return unit(value, u);

The 16 within the remify unit represents the base font size, in pixels, that rems should be calculated by. The usage and output looks as follows:

.smaller {


	.smaller {
		font-size: 13px;
		font-size: .8125rem;


Don't forget to set the base font size on the html element, usually 100%.  Also remember this mixin assumes a PX font is passed in, so if you aren't looking to use PX as backup, this isn't the solution for you.  If you do, however, there's no hurt in using these Stylus mixins!

Recent Features

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    Detect DOM Node Insertions with JavaScript and CSS Animations

    I work with an awesome cast of developers at Mozilla, and one of them in Daniel Buchner. Daniel's shared with me an awesome strategy for detecting when nodes have been injected into a parent node without using the deprecated DOM Events API.

Incredible Demos

  • By
    Pure CSS Slide Up and Slide Down

    If I can avoid using JavaScript for element animations, I'm incredibly happy and driven to do so.  They're more efficient, don't require a JavaScript framework to manage steps, and they're more elegant.  One effect that is difficult to nail down with pure CSS is sliding up...

  • By
    Image Manipulation with PHP and the GD Library

    Yeah, I'm a Photoshop wizard. I rock the selection tool. I crop like a farmer. I dominate the bucket tool. Hell, I even went as far as wielding the wizard wand selection tool once. ...OK I'm rubbish when it comes to Photoshop.


  1. You can also use this REM converter, which is really nice!

  2. I’ve been doing it px-ify style instead of remify!

    $default-font-size = 14px
      if unit(_size) == 'rem'
        font-size: (_size * $default-font-size)px
      font-size: _size
  3. The first sentence in this post cracks me up every time. Oh and if anyone wants to remify multiple values you can just:

    remify() {
        remified = ();
        for arg in arguments {
            push(remified, unit(arg/16, 'rem'));
        return remified;


    .foo {
        padding: remify(16px 32px 8px);


    .foo {
        padding: 1rem 2rem 0.5rem;
  4. Mohamed Elkebir

    It’s best to let this code work with multi/mixed-values:

    pixify(property, sizes, base = 16)
      pixels = ()
      rems = ()
      for j, i in sizes
        if typeof(j) is 'unit'
          push(pixels, unit(j * base, 'px'))
          push(rems, unit(j, 'rem'))
          push(pixels, j)
          push(rems, j)
      {property}: pixels
      {property}: rems


    pixify(border, 0.3 solid red)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!