HTML & CSS
Article

Sass Functions to Kick-Start Your Style Sheets

By Byron Houwens

CSS is used to style your content. Okay, that’s possibly the most obvious statement anyone working in the web can make these days, but it’s important because Sass, as a CSS preprocessor, is also then responsible for helping you style your content. It can do it just that little better.

In fact, Sass has a number of built-in functions that help make setting up styles for your project a lot quicker and easier.

Today we’ll look at just a few of them, ranging from manipulating color to number powers that can help us in making our own functions.

Darken and Lighten

Possibly two of the more well-known ones in this list, I’m going to count these as one since they do the same thing in different directions.

As the names suggest, darken and lighten will darken and lighten a color by a certain percentage respectively. You could use them on a buttons hover state or throughout a site to create hierarchy and would write them like so:

$main-color: #6dcff6;
$darker-color: darken($main-color, 20%);
$lighter-color: lighten($main-color, 20%);

The second argument in these two functions takes a percentage value by which to darken/lighten a color. This way you don’t have to look up the hex for a slightly lighter color every time you want an easy interaction state. For example, you could do this:

.brand-button {
    background: $main-color;
}

.brand-button:hover {
    background: $lighter-color;
}

.brand-button:visited {
    background: $darker-color;
}

Which compiles into this:

.brand-button {
    background: #6dcff6;
}

.brand-button:hover {
    background: #cdeffc;
}

.brand-button:visited {
    background: #0fafee;
}

Using these means that you could make an effective color palette within your styles that can remain consistent throughout the project. If, for instance, you have highlight and inactive state colors based off of a main brand color and your client decides to change their main color midway through development (it happens more than I care to admit…) you only have to change one value and see it cascade throughout the rest of a site.

Opacify and Transparentize

Another pair of functions, and sticking with colors, opacify and transparentize make colors more or less opaque respectively.

Personally, I’ve found these helpful when working with modals and other pop-ups where you might want the background content faded out.

$main-color: rgba(0, 0, 0, 0.5);
$opaque-color: opacify($main-color, 0.5);
$transparent-color: transparentize($main-color, 0.3);

Unlike darken and lighten, the second argument in these two functions needs to be a decimal between 0 and 1 instead of a percentage. These functions are helpful for colors based off of a single, main brand color that permeate throughout a project. And again, by including these functions, you’re able to make a change to the main color alone and the it will propagate accordingly.

You could use it like this:

.modal.focus {
    background: $main-color;
}

.modal.blur {
    background: $transparent-color;
}

.main-content {
    background: $opaque-color;
}

Which will compile into this:

.modal.focus {
    background: rgba(0, 0, 0, 0.5);
}

.modal.blur {
    background: rgba(0, 0, 0, 0.2);
}

.main-content {
    background: black;
}

You can also use fade-in and fade-out to create the same effect as these, as they’re aliases for opacify and transparentize.

Complement

As the name suggests, this will return the complement of any color that gets fed into it. This is especially useful if you’re trying to create a call-to-action on your page or view to create a bit of visual contrast. Again, this alleviates the need to go look the color up and can also hinge on that main brand color.

$main-color: #6dcff6;
$call-to-action: complement($main-color); //=> this will return #f6946d

Percentage

Sass’s numeric functions seem focused on helping you create your own functions or loops. Percentage, for instance, converts any number into a percentage like so:

width: percentage(0.16) //=> this will return 16%

As before, most usable values will be normalized ones (between 0 and 1). It can also take mathematical equations as arguments, allowing you more flexibility:

width: percentage(100px/50px); //=> this will return 200%

It doesn’t even care if you shove units like px onto the values. This makes it a candidate for easy typographic or responsive value conversion, where percentages are king. What a nice guy!

If

Sass comes with the ability to write normal if statements using “@if” and it does a great job in replicating what you’d find in a traditional programming language to create some fantastic conditional logic.

In addition, though, it also provides us with an inline if function! This if function works much like the ternary operator in a number of other programming languages:

.foo {
   width: if( 1 > 2, 400px, 500px); //=> this will return 500px
}

It accepts three arguments, the first being the condition to check, the second being the output if the condition is true, and the third being the output if the condition is false.

You could use this ternary function if you wanted to create variable-dependent conditions (if the width of an article element is bigger than an aside element, for instance) in your style sheets, though the possibilities with this one are a little more open-ended.

Personally, I find this more useful in responsively-focused projects. You could use this to change properties based on otherwise unrelated variables (like the font-size of an element depending on the width of its container) or even have properties rely on variables that change at different screen sizes or displays (like changing the background image of an element for retina screens).

Conclusion

Sass has a number of useful functions in its built-in arsenal, and these don’t even include the ones that Compass makes available to you, should you be using it. In the end, it’s about what kind of workflow makes sense for you to be using in your own work, depending on your project, team and deadlines.

Are there any Sass or Compass functions you’ve come across that you think should be added to the list or that you simply find useful in your projects? Let us know in the comments below.

  • http://www.danielcreid.com Daniel Reid

    If you read through this article and want to know more, check out the official Sass function documentation. There’s a lot of good stuff in there: http://sass-lang.com/documentation/Sass/Script/Functions.html

  • http://www.contriveitup.com/ Rupak

    Thanks…excellent info..didn’t knew about these until now!

    • Byron Houwens

      Great I’m glad it could be of help!

  • Rob_zelf

    Great tips. Didn’t know about the complement thing. Going to check the complete function documentation to see more.

  • Pina

    SASS gets better all the time, and these features (which I didn’t know about) would have saved me a bit of time, so thanks for the article. A question: is SASS going to get bloated? New features seem to be coming all the time, and it’s on its way to becoming quite a complex bit of software. Thanks Byron.

  • Byron Houwens

    I’m not a core contributor to it at all, so my answer will just be an educated guess at best, but I don’t think its bloat will ever be an issue. At the end of the day, we’re compiling it down into regular old CSS, which is what you serve and the user downloads, so in terms of performance it’s only the CSS you worry about.

    Also, CSS is not overly complicated, so Sass doesn’t really need overly complex features because it compiles down to a rather simple language anyway. But this depends on how you use it, so I guess it depends on how each person works as to whether it’ll be bloated or not. It may be an issue one day when servers can run Sass as is, no compiling, but I think that is still a while away.

    Just my ignorant opinion :)

  • Rab Simpson

    I’m trying to implement lighten() and instead of returning a lighter version of the original colour, it’s giving me ‘white’.

    This is my code:
    $adam_cyan: #56A9DE;
    $adam_cyan_light: lighten( $adam_cyan, 60% );

    Any ideas?

    • Rab Simpson

      It turns out mix() is what I was looking for.

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in Front-end, once a week, for free.