{"id":704,"date":"2013-02-22T17:36:55","date_gmt":"2013-02-22T23:36:55","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=704"},"modified":"2013-02-22T17:36:55","modified_gmt":"2013-02-22T23:36:55","slug":"use-a-css-preprocessor","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/use-a-css-preprocessor\/","title":{"rendered":"Use a CSS Preprocessor"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2013\/02\/preprocessors.jpg\" alt=\"Three popular CSS Preprocessors: Sass, Stylus, and Less\" width=\"600\" height=\"250\" class=\"alignnone size-full wp-image-705\" \/><\/p>\n<p>CSS Preprocessors have been around for a while now, but I didn\u2019t start seriously looking into them until about a year ago. I have loads of excuses for this: I was too busy, I already knew how to write CSS, cool kids write their CSS by hand&#8230; you get the idea. Basically, I didn\u2019t think yet another language like <a href=\"http:\/\/lesscss.org\/\">LESS<\/a>, <a href=\"http:\/\/learnboost.github.com\/stylus\/\">Stylus<\/a> or <a href=\"http:\/\/sass-lang.com\/\">Sass<\/a> would have anything to offer me.<\/p>\n<p>I was wrong, and CSS Preprocessors are awesome.  Here are three good reasons why. <!--more--><\/p>\n<h3>You Already Know A Lot<\/h3>\n<p>If you already know a decent amount of CSS, guess what? Congratulations! You also already know a good portion of LESS and Sass as well.<\/p>\n<p>In both languages, CSS is perfectly valid to write. So when you want to bold a tag, this is what your Sass would look like:<\/p>\n<pre lang=\"css\">strong { font-weight: bold; }<\/pre>\n<p>That\u2019s 100% homegrown CSS there, folks. Because Sass and LESS <strong>are CSS<\/strong> fundamentally. Just like you can write normal JavaScript in your jQuery, you can write CSS in your preprocessor of choice.<\/p>\n<p>This is awesome because it means you don\u2019t have to spend hours learning a new syntax just to spit out the clich\u00e9d \u201cHello World\u201d page. You can write what you know, and add in the new syntax as you learn it.<\/p>\n<h3>CSS3 Is <em>Hard<\/em><\/h3>\n<p>So here\u2019s the thing: I build websites for a living and I write about building websites in my free time. And yet when it comes time to craft a cross-browser CSS linear gradient on my site, I\u2019m pulling up Google just like 99.9% of the other developers out there. Because with the extra power we have with CSS these days comes great complexity.<\/p>\n<p>Take a look at this CSS for a simple blue background gradient:<\/p>\n<pre lang=\"css\">.gradient {\n  background: -moz-linear-gradient(top, #1e5799 0%, #7db9e8 100%);\n  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(100%,#7db9e8));\n  background: -webkit-linear-gradient(top, #1e5799 0%,#7db9e8 100%);\n  background: -o-linear-gradient(top, #1e5799 0%,#7db9e8 100%);\n  background: -ms-linear-gradient(top, #1e5799 0%,#7db9e8 100%);\n  background: linear-gradient(to bottom, #1e5799 0%,#7db9e8 100%); \n}<\/pre>\n<p>And no, I didn\u2019t write that by hand. I can\u2019t remember the intricacies of that sort of syntax. I nabbed it from <a href=\"http:\/\/www.colorzilla.com\/gradient-editor\/\">ColorZilla<\/a> like a sane human being.<\/p>\n<p>And yet, what happens when we use Sass (with its fantastic helper, <a href=\"http:\/\/compass-style.org\/\">Compass<\/a>)? Magic:<\/p>\n<pre lang=\"css\">.gradient {\n  @include background-image(linear-gradient(#1e5799 0%, #7db9e8 100%));\n}<\/pre>\n<p>One line of code. And it\u2019s simple enough I can memorize the syntax. And then when I compile my Sass, it spits out CSS just like the CodeZilla-spawned code soup above.<\/p>\n<h3>Variables and Mixins ROCK<\/h3>\n<p>Remember when you very first started hearing about CSS, and the promise of how it was going to simplify your development? You write one simple rule, and the resulting style magically cascades down through your document. Brilliant!<\/p>\n<p>But why is it that the mojo of the cascade still leaves us repeating chunks of code throughout our site? Why do we find ourselves scattering colors, padding widths and font sizes in a half dozen places in the same stylesheet? <\/p>\n<p>Preprocessors solve that by giving us variables. We&#8217;ll extend our gradient example:<\/p>\n<pre lang=\"css\">$lightBlue: #7db9e8;\n$darkBlue: #1e5799;\n.gradient {\n  @include background-image(linear-gradient($darkBlue 0%, $lightBlue 100%));\n}\na { color: $darkBlue; }<\/pre>\n<p>We define a variable at the top of our stylesheet, then use it wherever we want. Now when your client comes back and says he wants that blue to have a <em>teensy bit<\/em> more green in it, you don\u2019t have to find it in a half-dozen places. Change the variable and be done with it.<\/p>\n<p>Mixins can be even more helpful. In Sass they are particularly powerful, acting almost like functions. If you find yourself using the same set of rules repetitively, mixins are for you. Here\u2019s a super simple mixin I use on CSS Newbie: <\/p>\n<pre lang=\"css\">@mixin inline-block {\n  display: inline-block;\n  *display: inline;\n  zoom: 1;\n}<\/pre>\n<p>This is a fix for inline-block in IE7 (which doesn\u2019t understand inline-block, but will treat inline elements with layout like an inline-block element anyway). Any time I want an element to be inline-block, I use this mixin, like so:<\/p>\n<pre lang=\"css\">.site-logo {\n  @include inline-block;\n}<\/pre>\n<p>And now my inline-block elements work all the way back to IE7, without me having to write out a bugfix each time.<\/p>\n<h3>The List Goes On and On<\/h3>\n<p>These are just three of the <strong>many<\/strong> reasons to use a CSS Preprocessor. Others include code nesting (which just makes sense, dammit) and built-in compression to reduce file size and speed up your site.<\/p>\n<p>I\u2019m not going to tell you here which preprocessor to use. I\u2019ve used <a href=\"http:\/\/lesscss.org\/\">LESS<\/a> in the past and mostly use <a href=\"http:\/\/sass-lang.com\/\">Sass<\/a> currently. In my opinion LESS is a little easier to learn and Sass is a little more powerful, particularly when paired with <a href=\"http:\/\/compass-style.org\/\">Compass<\/a>. And there are others out there (<a href=\"http:\/\/learnboost.github.com\/stylus\/\">Stylus<\/a> has a strong following but I\u2019ve not used it). But what I <strong>will<\/strong> tell you is to use one. There are lots of reasons to do so, and not very many good reasons not to.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>CSS Preprocessors have been around for a while now, but I didn\u2019t start seriously looking into them until about a year ago.  I didn\u2019t think yet another language like LESS, Stylus or Sass would have anything to offer me. I was wrong, and CSS Preprocessors are awesome.  Here are three good reasons why. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/use-a-css-preprocessor\/\" title=\"Click to read 'Use a CSS Preprocessor'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":2635,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[193],"tags":[234,49,295,296,141,138,354,140],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/704"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=704"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/704\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/2635"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}