Open book with bookmark

Why Sass?

A note from the editors: We are pleased to present you with an excerpt from Sass For Web Designers by Dan Cederholm, available now from A Book Apart.

I was a reluctant believer in Sass. I write stylesheets by hand! I don’t need help! And I certainly don’t want to add extra complexity to my workflow. Go away!

Article Continues Below

That was the thinking anyway. But the reality is that Sass (and other CSS preprocessors) can be a powerful ally—a tool that any style-crafter can easily insert into their daily work. It took me a while to come around, but I’m sure glad that I did.

And that’s the reason I wanted to write this little book. To share how I’ve been able to use Sass to be more efficient, while maintaining the process I’ve become comfortable with from writing CSS for the last ten years. I had many misconceptions about Sass that prevented me from giving it a go, initially. I was worried I’d have to completely alter the way I write and manage stylesheets. As CSS can be fragile at times, it’s understandable for its authors to be somewhat protective about their creation. Can I get an amen?

Ahem.

So, I’m here to show you how Sass doesn’t have to disrupt your process and workflow, and how it can make your life easier. I’ll demonstrate the various aspects of Sass, how to install it, how to use it, and how it’s helped me in my own projects. With any luck, I just might make you a believer as well.

The Sass elevator pitch#section2

Ever needed to change, say, a color in your stylesheet, and found that you had to find and replace the value multiple times? Don’t you wish CSS allowed you to do this?

$brand-color: #fc3;
a {
color: $brand-color;
}
nav {
	background-color: $brand-color;
}

What if you could change that value in one place and the entire stylesheet reflected that change? You can with Sass!

Or how about repeated blocks of styles that are used in various locations throughout the stylesheet?


p {
margin-bottom: 20px; font-size: 14px; line-height: 1.5;
}
footer {
	margin-bottom: 20px;
	font-size: 14px;
	line-height: 1.5;
}

Wouldn’t it be fantastic to roll those shared rules into a reusable block? Again, defined only once but included wherever you needed them.


	@mixin default-type {
	margin-bottom: 20px;
	font-size: 14px;
	line-height: 1.5;
}
p {
@include default-type;
}
footer {
	@include default-type;
}

That’s also Sass! And those two extremely simple examples barely scratch the surface as to how Sass makes authoring stylesheets faster, easier, and more flexible. It’s a welcome helper in the world of web design, because anyone that’s created a website knows…

CSS is hard#section3

Let’s face it: learning CSS isn’t easy. Understanding what each property does, how the cascade works, which browser supports what, the selectors, the quirks, and so forth. It’s not easy. Add on top of that the complexity of the interfaces we’re building these days, and the maintenance that goes along with that and—wait, why are we doing this again? It’s a puzzle, and some of us enjoy the eventual completion.

Part of the problem is that CSS wasn’t originally designed to do the things we do with it today. Sure, progress is moving along at a nice clip thanks to rapid browser innovation and implementation of CSS3 and beyond. But we still need to rely on techniques that are, for all intents and purposes, hacks. The float property, for example, was designed to simply align an image within a block of text. That’s it. Yet we’ve had to bend that property to lay out entire interfaces.

Our stylesheets are also immensely repetitive. Colors, fonts, oft-used groupings of properties, etc. The typical CSS file is an extremely linear document—the kind of thing that makes an object-oriented programmer want to tear their hair out. (I’m not an object-oriented programmer, but I have very little hair left. Read into that as you may).

As interfaces and web applications become more robust and complex, we’re bending the original design of CSS to do things it never dreamed of doing. We’re crafty like that. Fortunately, browser makers adopt new CSS features far more rapidly these days, with more efficient and powerful properties and selectors that solve the problems today’s web poses. Features like new layout options in CSS3, border-radius, box-shadow, advanced selectors, transitions, transforms, animation, and so on. It’s an exciting time. And yet, there’s still a lot missing from CSS itself. There are holes to be plugged, and the life of a stylesheet author should be a lot easier.

The DRY principle#section4

If we peer into the world of software engineering (and I much prefer to peer than hang out and get comfortable there), we can quickly see how organization, variables, constants, partials, etc., are an ingrained, crucial way of working for folks building complex systems.

You may have heard of the “don’t repeat yourself” (DRY) principle. Coined and defined by Andy Hunt and Dave Thomas in their book, The Pragmatic Programmer (http://bkaprt.com/sass/1/), DRY declares:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

The idea is that duplicating code can cause failure and confusion for developers (http://bkaprt.com/sass/2/). It’s common sense as well: write commonly-repeated patterns once, and reuse those bits throughout the application. It’s more efficient and far easier to maintain code this way.

CSS is anything but DRY. At times, it drips with repeated rules, declarations, and values. We’re constantly writing the same snippets of code for colors, fonts, and frequently-used patterns of style throughout our stylesheets. One look through a decent-sized CSS file, and a DRY software developer will weep, first with bewilderment, then frustration.

“How the !@#$ do you maintain this?!” they’ll ask.

“Have I also told you about IE bugs?” you’ll reply with a bit of self-loathing.

So why is CSS so difficult to work with?#section5

We can gather a hint of understanding why CSS has had its syntax limitations over the years from an essay by CSS co-inventor, Bert Bos (http://bkaprt.com/sass/3/):

CSS stops short of even more powerful features that programmers use in their programming languages: macros, variables, symbolic constants, conditionals, expressions over variables, etc. That is because these things give power-users a lot of rope, but less experienced users will unwittingly hang themselves; or, more likely, be so scared that they won’t even touch CSS. It’s a balance. And for CSS the balance is different than for some other things.

The original architects of CSS were concerned with adoption. They (rightfully) wanted as many people as possible creating websites. They wanted CSS to be powerful enough to style web pages and separate content from presentation, while being easy to understand and use. I can certainly respect that. At the same time, we have work to do, and that work is getting more complicated, more nuanced, and more challenging to maintain and to future-proof.

Fortunately, there are options to help us out here, and one of them is Sass.

What is Sass?#section6

Sass is a CSS preprocessor—a layer between the stylesheets you author and the .css files you serve to the browser. Sass (short for Syntactically Awesome Stylesheets) plugs the holes in CSS as a language, allowing you to write DRY code that’ll be faster, more efficient, and easier to maintain.

The Sass website (http://bkaprt.com/sass/4/) describes itself succinctly:

Sass is a meta-language on top of CSS that’s used to describe the style of a document cleanly and structurally, with more power than flat CSS allows. Sass both provides a simpler, more elegant syntax for CSS and implements various features that are useful for creating manageable stylesheets.

So while normal CSS doesn’t yet allow things like variables, mixins (reusable blocks of styles), and other goodies, Sass provides a syntax that does all of that and more—enabling “super functionality” in addition to your normal CSS. It then translates (or compiles) that syntax into regular ol’ CSS files via a command-line program or web-framework plugin.

More specifically, Sass is an extension of CSS3, and its SCSS (“Sassy CSS”) syntax—which we’ll talk about in just a moment—is a superset of CSS3. Meaning, any valid CSS3 document is a valid SCSS document as well. This is integral to Sass being something you can “ease into.” Getting started with Sass syntax is painless, and you can use as little or as much as you’d like. Which also means converting an existing stylesheet from CSS to SCSS can be done in stages, as you learn and pick up more of Sass’s functionality.

Later, when you’ve become fluent with Sass (and it won’t take long), it really does feel like a natural extension of CSS—as if it’s filling holes we all wish were filled by the CSS spec itself. This is why, once I started using Sass, I never once thought it was awkward or laborious—it just feels like CSS should feel. Once you try it, you’ll likely stick with it permanently.

Furthermore, Sass is helping CSS get better. By fast-tracking certain features that aren’t currently possible without the help of a preprocessor, it’s giving CSS authors real-world implementation and feature experimentation. When and if it makes sense, certain Sass functionality could very well inform future CSS specifications.

Sass misconceptions#section7

I mentioned earlier that I was reluctant to try Sass. This was partly due to a lot of misconceptions I had prior to using it. Do I need to know Ruby or advanced command-line shenanigans? Will I need to completely change the way I’ve been writing stylesheets? Will the CSS it outputs be bloated and unreadable?

Thankfully, the answer is “nope” for each of those questions, of course—but I do hear them pop up whenever someone mentions Sass on various internet channels. Let’s clear up a few things.

I’m afraid of the command line!#section8

I am by no means a command-line expert, but I’ve learned a bit here and there over the years—just enough to get me into trouble. I’m not afraid to traverse the file system with it or use Git commands, etc.

That said, I sympathize with designers and front-end developers who don’t want to go there. There’s a command-line phobia that exists among some folks. For Sass, there’s very little command-line action required—in fact, a single command is all you need to grasp. Additionally, there are apps and web frameworks that will obviate the need for the command line. (I’ll be introducing those in the next chapter).

So, if you’re a command-line avoider, don’t let that stop you from trying Sass!

I don’t want to change the way I write CSS!#section9

This was the misconception that I suffered from. I’m particular about the way my stylesheets are set up and organized. There’s a certain amount of craft that goes into the document. But remember, since the SCSS syntax is a superset of CSS3, you don’t have to change anything about the way you write CSS. Commenting, indenting, or not indenting, all your formatting preferences can remain the same when working in .scss files. Once I realized this, I could dive in without fear.

I don’t want Sass to change the way I design!#section10

On the flip side, Sass won’t solve all of your problems or cure your bad habits. Inefficient, bloated stylesheets can be just as inefficient and bloated when using Sass. Good organization and smart thinking still apply here. In fact, there are instances where Sass can magnify bad practices, and we’ll go into that a bit as well. But when used properly and intelligently, Sass can be such a massive assist in creating websites.

Okay. Now that we have the particulars out of the way, let’s start having some fun. I think you’ll be amazed at what Sass can do. In the next chapter, we’ll set up our workflow—how Sass can fit into your process and how easy it is to use the command-line or apps. Let’s get Sassing, people.

About the Author

Dan Cederholm

Dan Cederholm is a designer, author, and speaker living in Salem, Massachusetts. He’s the Co-Founder of Dribbble, a community for designers, and Founder of SimpleBits, a tiny design studio. A long-time advocate of standards-based web design, Dan has worked with YouTube, Microsoft, Google, MTV, ESPN and others. He’s written several popular books about web design, and received a TechFellow award in early 2012. He’s currently an aspiring clawhammer banjoist and occasionally wears a baseball cap.

69 Reader Comments

  1. I really love to use it, better forever. Bring the word of variable,conditional and other to CSS. Easy to use, easy to maintain…
    Why don’t use SCSS today ^^?
    (My feeling after 5 months used it^^)

  2. Good to see this. One of your books helped me years ago master CSS, and this past year I’ve fully embraced using SASS as well. It IS complex the deeper you go, but for the examples you’ve listed, I think it’s easy for users to make the switch.

  3. I’ve been a LESSCSS user for some time. It’s been a real change in efficiency, organization, code reducing, modularization… and fun. I gave SASS a try. (Yeah. I had a @ extend hangover too. Its like tequila. I don’t remember what had happened but was a great party…) Don’t know why the SASS hype started so loud these days. Maybe something good will come with that. Maybe CSS4 will come with SASS and batteries included. Just don’t know. But will be good for us anyway. I just hope it become funnier and simple as LESSCSS. Write @ mixin and @ include for every mixin is boring.

  4. Just like Dan it took me a while to start using a preprocessor—now it is a key part of my workflow (I use LiveReload).

    If you are moving to SASS, I would suggest you check out the .sass syntax. It does take some getting used to (compared to the more common .scss syntax), but the readability and simplicity offered by .sass is helpful—especially when working on complex projects.

  5. The Thesis 2.X framework has had a CSS preprocessor built into it. It also lets you separate reusable code in the skin editor from custom CSS. Both use the same set of variables.

    I have found it very efficient to make CSS changes only to the custom CSS, leaving the basic skin alone.

  6. I use Less for two reasons:

    1) It can run in the browser (no compile step in development)
    2) The compiler can run in node. The whole Ruby stack is way to slow.

    Also I’m not a CSS framework developer, so I don’t need the extra features that Sass admittedly has. Apparently everybody else seems to have no problem with the slowness of ruby. At CSSConf.eu I was the only non-Sass user in the room.

  7. Some Frameworks allow you to build HTML5 and CSS3 projects fast and most importantly responsively. I tried Foundation5 and ended up banging my head against the wall(desk) and just used the simplest partsof the framework instead of trying to learn all the “push-” and complicated alteration markup. Ditto for YII and Twitter. Too much to learn when I can just type in what I want.
    Instead I now build my own framework with either fixed divs or percentage divs; or both then apply a Margin Master Style Sheet that I wrote which eliminates learning ANY other markup that compensates for a framework being over complex—demoizing itself by over-design. Margin Master I hope catches on because it is the simplest and easiest way to create space between elements and divs without having to learn any [one elses] syntax. “marg-” is the class tag and (for example) marg-t1 is: margin: [top 1em]… it goes on forever with marg-b (bottom), marg-l (left) and several center and text alignment syntax starndards (hope to be… catch on standards).

    Webpage CSS would include reset, style, margins and custom. No java, no learning just l1 thru 9 or t1 thru9 etc. You have to see it to believe in it. http://sourceforge.net/projects/marginsmaster/ Larry Judd tradesouthwest.com

  8. Sorry but I’m not jumping on this “believer” train. I don’t find CSS hard, and I’m not impressed with your opening examples. In your first examples, you say “wouldn’t it be great if you could just change the color in one place…” but no, I don’t think that would be great at all. That means I lose the flexibility of changing things later and having the header color be something else. I’d rather just use CSS classes and IDs the way they were meant to be used. It works, it’s easy, it’s efficient, and it doesn’t take me long to write regular old CSS. Maybe if I found it “hard” like you do then I might look at adding a crutch into the mix.

    And what about maintenance? If developer A used SASS, but developer B needs to make a change. Well, sucks to be developer B if he/she is unfamiliar with SASS or *doesn’t like it* or something else.

    I think all these people who like SASS are not crafting uniquely designed websites or web apps, but rather making cookie-cut template sites that all look the same except for a few colour changes here and there. Enter SASS.

    You can keep it, thanks. I do my programming in JS. CSS has a lot of things going for it these days in modern browsers, please don’t ruin it by insisting that developers believe in your pre-processing evangelism. If you’re happy using it, go for it. But it’s not REQUIRED for good quality CSS coding.

  9. Hey, I think Sass or Less are OK nevertheless it takes a sub-process to compile the code and previous steps just to have some more useful functions. I already used Sass once or twice but in my opinion all the fuss needed to setup it up doesn’t pay the output. I see Sass/Less as a potential evolution of CSS just like Jquery was and is for Javascript although the gain on the last one is way more substantial and we just need a link to the CDN, easy!

    Although I don’t believe in the technology I enjoyed the article,
    Best regards, Dessain Saraiva.

  10. Like others say,

    Sass or other CSS compilers can be an extra step in your workflow. It does adding a little more to the learning curve when you start using Sass functions, mixins and other goodies.

    However, if you take the time to set up a good file structure (using _partials) to organize your styling rules; you are bound to not only enjoy using technology like Sass, you’ll depend on it.

  11. I’ve been pushing myself lately to write very lean css. The examples provided at the top would be producing serious bloat. To write lean css requires good mockups that take reusable elements into account, and good understanding of css. Bad mockups produce big bloat. I’m also a big advocate of mixing elements in the markup, that way you keep your css rules very lean, and your css file very clean. Mixing css classes in the mockup is sort of the same as sass does in css itself. It is harder to write lean css in using preprocessors because preprocessors add another layer of obstruction, and you have to think harder to write good css.

    Secondly, I never understood why changing color in sass is such a good example of the power of sass:
    $brand-color: #fc3;
    a {color: $brand-color;}

    To me this above sass code is not even a good example of lean css output. What you’d end up is bunch of css rules where the same color is repeated over and over again. I’m sure there is a leaner way of writing this same example in sass without producing bloat. Someone please chime in.

    Look, if you need to change color after you’ve coded the site, you have a serious communication problem somewhere in your design process. That’s what mockups are for. I thought you’re supposed to agree with your client on color, and all the details of your design before you jump into sass. But apparently a lot of folks change color after color. Why? I do not know.

    SASS has its uses, as do other preprocessors, but my preferred one is Stylus. Very easy to setup, allows you to write bracket-less rules, and extend on the existing regular stylesheets.

  12. Thanks for the Article. Is your book on Safari books online? I can’t find it. Also, do you know of other books, videos on sass. Currently there appears to be only 3 books on sass on safari books online, and the rest refer to sass in single chapters. Thanks!

  13. I just learned CSS (yet to start my first project ). Would you advise me to use learn SASS now or play more with CSS in a few projects and then learn SASS ?

    All the pro-developers/designers kindly pour in your thoughts.

  14. The greatest strength of SASS (or any pre-processor) for me is how nicely it plays with version control software. Having multiple front-end developers working on a project without css merge conflicts (because everyone’s working on isolated scss file) saves a lot of headaches. Likewise, tracking changes over time is a lot easier when the files are broken up in to modules and properly commented.

  15. You stated…

    What if you could change that value in one place and the entire stylesheet reflected that change? You can with Sass!

    Or how about repeated blocks of styles that are used in various locations throughout the stylesheet?

    I read this and I had to comment on this first. I’m going to force myself to read the rest of this article, but this threw me off… I can change the value in one place and have it trickle down through my entire site…. with a class! Based on the description you gave, with a color, or changing a block of styles… I can do this with a class.

    At first glance, I’m not sure what the real benefit of SASS is… I could use it, but I’m more motivated to write something I know. Again, I’ll keep reading, but this almost made me leave the page.

  16. I appreciate with this and if you have some more information please share it with me. This is really good work from your side. I strongly recommend you keep doing this kind of work, and I hope you will have many entries or more. http://khelomcx.com/

Got something to say?

We have turned off comments, but you can see what folks had to say before we did so.

More from ALA

I am a creative.

A List Apart founder and web design OG Zeldman ponders the moments of inspiration, the hours of plodding, and the ultimate mystery at the heart of a creative career.
Career