Useful SASS tidbits
I've been working a lot lately with SASS (or SCSS whatever) and there are some pretty neat tricks that make writing CSS seem almost programatic (we all know it's not).
For example it's possible to check the lightness of an elements background, then dictate the text colour depending on that outcome (example sourced from Foundation).
@mixin button($bg:$default-bg) { $bg-lightness: lightness($bg); @if $bg-lightness > 70% { color: $button-font-color-dark; &:hover, &:focus { color: $button-font-color-dark; } } @else { color: $button-font-color-light; &:hover, &:focus { color: $button-font-color-light; } } }
Another useful tidbit is using the for loop.
@for $i from 0 through 2 { .item-#{$i} { border: 1px solid gold; } } @for $i from 3 through 5 { .item-#{$i} { border: 1px solid silver; } } @for $i from 6 through 8 { .item-#{$i} { border: 1px solid bronzw; } }
Which then compiles into:
.item-0, .item-1. .item-2 { border: 1px solid gold; } .item-3, .item-4. .item-5 { border: 1px solid silver; } .item-6, .item-7. .item-8 { border: 1px solid bronze; }
There are a lot more useful tools within the SASS DSL, I'd suggest spending 15 minutes or so just reading through the docs.
Written by Jack Callister














