October 8, 2015 8:40 am

Getting Started with Sass Basics Part 2

Mathematical operations,  interpolations and for loops

With Sass, you can perform all kinds of mathematical operations. You can modify colors by adding or subtracting from them with the standard arithmetic operators.

sass-basics

If we have the color from the previous article:

We can modify it as we please before using it:

The resulting CSS will contain the following hex color value:

We are not limited to subtracting a hexadecimal color value, we can subtract/add rgb value no matter if we are performing the operation on another rgb value or on a hex value.

The above snippet will result in the following CSS code:

Assume that we have the following markup:

We add some basic styles to these individual boxes:

Now assume that we had many more boxes and we wanted to loop over each and gradually increase their font-size. In pure CSS this is not possible and you would have to do everything manually. However, this can be done with Sass:

You are probably familiar with the for loop from JavaScript/PHP but the syntax is different in Sass. You type @for and then provide a variable name (where the current iteration number will be stored) and then choose from what number to what number you want it to loop – in our case from 1 through 3.

If you want to add a variable to a place other than a property’s value – such as the property name or the selector itself you would have to interpolate your string. This is done with the following syntax: #{$someVariable}. You can see in our case that we have interpolated the $i variable to the nth-of-type pseudo-selector so that we can provide a different value for each div.

On the next line, you can see that we have multiplied the pixel value by the iteration counter. You can add/subtract/multiply/divide pixel values and other units with an arbitrary number.

Now, as we add new boxes their font-size will automatically increase as you can see from the picture below… and it will probably become huge too fast.

Randomness and concatenation

If you want the font-size to be in the hands of God (more likely in the machine) Sass allows you to generate random numbers.

We change our font-size property to hold a random number between 1 and a maximum (top) value you provide, in our case we provide a top value of 60 and add 5 to it so the font would not be as small as 1px. Sass also allows string concatenations (with or without quotes) so in the end we just add px in order to have a valid value for the font-size property.

Now, each time we recompile our Sass code the boxes will have a different font-size. Here is a sample result (CSS):

Conclusion

Be sure to go again in http://sassmeister.com/ and play around with what you have learned so far. Undoubtedly, there are much better uses to what we have been showing that you can explore.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories: