9Calculating a Simple Layout

Sass allows you to do calculations on the fly and in your document: you can easily type width: 12px * 0.5; in your code!

OK, OK—we admit that’s not terribly useful. But it is once you throw variables into the mix. Once you’ve defined a variable, Sass allows you to perform basic operations on that variable using standard operators for adding, subtracting, multiplying, and dividing (+, -, *, and /). The operators will be familiar to anyone who has done any amount of programming before.

You could say something like width: $page_width * 0.1 as a way to avoid hard-coding pixel values. When the CSS is compiled, this will be precalculated and will print out an exact width in pixels.

You can now do the previously laborious ...

Get Pragmatic Guide to Sass 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.