Search code examples
csssasscompass-sass

Possible to create random numbers in SASS / Compass?


I'm working on a project where I utilize SASS and Compass and need to somehow come up with some random numbers in the CSS file. Specifically, I need some floating point numbers between 0 and 1. Is this possible with SASS and Compass alone?


Solution

  • This is very possible if you create a sass function since sass is ruby its as easy as opening the functions module and injecting your random function

    module Sass::Script::Functions
      module MyRandom
        def random
          rand(1.0)
        end
      end
      include MyRandom
    end
    

    require the file after sass has loaded

    Then in your stylesheet

    $my-randome-number: random();