Getting Started with SASS for Drupal and Zen, Part II
In part one of "Getting Started with SASS for Drupal and Zen," we went over getting your environment set up to work with SASS.
If you followed the instructions in part one, you should have SASS/Compass, Zen, and your sub-theme installed. Your theme will be installed in sites/all/YOUR THEME NAME
.
Test the Install
Let's test to see if SASS is installed and compiling. Use your toolkit to compile your SASS directory or run compass watch
from the command line in your theme directory. You should see the following output.
>>> Compass is watching for changes. Press Ctrl-C to Stop
To see more Compass commands, you can run Compass -h
.
Open your Drupal site in your browser. Now that we are polling for changes with Compass, let's add the following to style.scss
to see our changes being applied. After you save your change, refresh your page and you should see the difference.
<span class="nt">body</span> <span class="p">{</span> <span class="k">background</span><span class="o">:</span> <span class="m">#000</span><span class="p">;</span> <span class="k">color</span><span class="o">:</span> <span class="m">#fff</span><span class="p">;</span><span class="p">}</span>
Compass will also output the overwritten files in your console if you are using command line to run it. It's okay to delete the CSS you added, so things will appear like the default Zen theme.
SASS Primer
If you haven't used SASS, prepare to be hooked on it. Some advantages of SASS include DRY (Don't Repeat Yourself), CSS, function (mixins) for repetitive and lengthy blocks of CSS, and the ability to extend common styles.
Variables
Variables in SASS start with a '$'. Use variables to define values you will use throughout your stylesheets. For example, let's define our color palette in _init.scss
. There is a commented section for colors. You can drop them in there. I'm going to grab this zen 2 palette from Kuler.
<span class="nv">$sand</span><span class="o">:</span> <span class="mh">#b0ae9e</span><span class="p">;</span><span class="nv">$brown</span><span class="o">:</span> <span class="mh">#424345</span><span class="p">;</span><span class="nv">$white</span><span class="o">:</span> <span class="mh">#fafeff</span><span class="p">;</span><span class="nv">$seagreen</span><span class="o">:</span> <span class="mh">#9dbec7</span><span class="p">;</span><span class="nv">$wetsand</span><span class="o">:</span> <span class="mh">#b0a092</span><span class="p">;</span><span class="nv">$red</span><span class="o">:</span> <span class="mh">#ff0000</span><span class="p">;</span><span class="nv">$gray</span><span class="o">:</span> <span class="mh">#a1a1a1</span><span class="p">;</span>
Now these colors can be used everywhere in our stylesheets without having to write the hex value each time.
Nesting
In typical CSS fashion, we would write a style like this:
<span class="nt">a</span> <span class="p">{</span> <span class="k">color</span><span class="o">:</span> <span class="m">#9dbec7</span><span class="p">;</span> <span class="k">text-decoration</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span><span class="p">}</span><span class="nt">a</span><span class="nd">:hover</span> <span class="p">{</span> <span class="k">color</span><span class="o">:</span> <span class="m">#424345</span><span class="p">;</span> <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">transition</span><span class="o">:</span> <span class="k">color</span> <span class="m">0</span><span class="o">.</span><span class="m">5</span> <span class="n">ease</span><span class="o">-</span><span class="n">out</span> <span class="m">0.5s</span><span class="p">;</span> <span class="o">-</span><span class="n">moz</span><span class="o">-</span><span class="n">transition</span><span class="o">:</span> <span class="k">color</span> <span class="m">0</span><span class="o">.</span><span class="m">5</span> <span class="n">ease</span><span class="o">-</span><span class="n">out</span> <span class="m">0.5s</span><span class="p">;</span> <span class="o">-</span><span class="n">o</span><span class="o">-</span><span class="n">transition</span><span class="o">:</span> <span class="k">color</span> <span class="m">0</span><span class="o">.</span><span class="m">5</span> <span class="n">ease</span><span class="o">-</span><span class="n">out</span> <span class="m">0</span><span class="o">.</span><span class="m">5</span><span class="p">;</span> <span class="n">transition</span><span class="o">:</span> <span class="k">color</span> <span class="m">0</span><span class="o">.</span><span class="m">5</span> <span class="n">ease</span><span class="o">-</span><span class="n">out</span> <span class="m">0.5s</span><span class="p">;</span><span class="p">}</span>
With SASS, we can nest the style like this:
<span class="nt">a</span> <span class="p">{</span> <span class="na">color</span><span class="o">:</span> <span class="nv">$seagreen</span><span class="p">;</span> <span class="na">text-decoration</span><span class="o">:</span> <span class="no">none</span><span class="p">;</span> <span class="k">&</span><span class="nd">:hover</span> <span class="p">{</span> <span class="na">color</span><span class="o">:</span> <span class="nv">$brown</span><span class="p">;</span> <span class="na">-webkit-transition</span><span class="o">:</span> <span class="no">color</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span> <span class="n">ease-out</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span><span class="p">;</span> <span class="na">-moz-transition</span><span class="o">:</span> <span class="no">color</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span> <span class="n">ease-out</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span><span class="p">;</span> <span class="na">-o-transition</span><span class="o">:</span> <span class="no">color</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span> <span class="n">ease-out</span> <span class="mi">0</span><span class="mf">.5</span><span class="p">;</span> <span class="na">transition</span><span class="o">:</span> <span class="no">color</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span> <span class="n">ease-out</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span><span class="p">;</span> <span class="p">}</span><span class="p">}</span>
The ampersand represents the outer anchor selector. Also, notice how we are relying on the variables we defined for the colors instead of using hex values.
Mixins and Extends
Mixins
Let's clean up that transition by writing a mixin for it.
<span class="k">@mixin</span><span class="nf"> transition</span><span class="p">(</span><span class="nv">$property</span><span class="o">,</span> <span class="nv">$duration</span><span class="o">,</span> <span class="nv">$easing</span><span class="p">)</span> <span class="p">{</span> <span class="na">-webkit-transition</span><span class="o">:</span> <span class="nv">$color</span> <span class="nv">$duration</span> <span class="nv">$easing</span><span class="p">;</span> <span class="na">-moz-transition</span><span class="o">:</span> <span class="nv">$color</span> <span class="nv">$duration</span> <span class="nv">$easing</span><span class="p">;</span> <span class="na">-o-transition</span><span class="o">:</span> <span class="nv">$color</span> <span class="nv">$duration</span> <span class="nv">$easing</span><span class="p">;</span> <span class="na">transition</span><span class="o">:</span> <span class="nv">$color</span> <span class="nv">$duration</span> <span class="nv">$easing</span><span class="p">;</span><span class="p">}</span>
Now we can rewrite the anchor style and include the transition mixin.
<span class="nt">a</span> <span class="p">{</span> <span class="na">color</span><span class="o">:</span> <span class="nv">$seagreen</span><span class="p">;</span> <span class="na">text-decoration</span><span class="o">:</span> <span class="no">none</span><span class="p">;</span> <span class="k">&</span><span class="nd">:hover</span> <span class="p">{</span> <span class="na">color</span><span class="o">:</span> <span class="nv">$brown</span><span class="p">;</span> <span class="k">@include</span><span class="nd"> transition</span><span class="p">(</span><span class="no">color</span><span class="o">,</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span><span class="o">,</span> <span class="n">ease-out</span><span class="o">,</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span><span class="p">);</span> <span class="p">}</span><span class="p">}</span>
Keep in mind that Compass already provides some great cross-browser mixins for CSS3. Style transition is one of them.
Extends
SASS lets you inherit common styles. A practical example is styling buttons. Buttons might have common styling, but differ in color or size.
<span class="c1">// This is a SASS comment</span><span class="cm">/* This is also a comment */</span><span class="c1">// Our default button</span><span class="nc">.button</span> <span class="p">{</span> <span class="na">background</span><span class="o">:</span> <span class="nv">$seagreen</span><span class="p">;</span> <span class="na">padding</span><span class="o">:</span> <span class="mi">1</span><span class="kt">em</span><span class="p">;</span> <span class="na">border</span><span class="o">:</span> <span class="mi">1</span><span class="kt">px</span> <span class="no">solid</span> <span class="nv">$seagreen</span><span class="p">;</span><span class="p">}</span><span class="nc">.primary</span> <span class="p">{</span> <span class="k">@extend</span> <span class="nc">.button</span><span class="o">;</span> <span class="nt">padding</span><span class="nd">:</span> <span class="nt">1</span><span class="nc">.5em</span> <span class="nt">2em</span><span class="o">;</span><span class="p">}</span><span class="nc">.warn</span> <span class="p">{</span> <span class="k">@extend</span> <span class="nc">.button</span><span class="o">;</span> <span class="nt">background</span><span class="nd">:</span> <span class="err">$</span><span class="nt">red</span><span class="o">;</span><span class="p">}</span><span class="nc">.disabled</span> <span class="p">{</span> <span class="k">@extend</span> <span class="nc">.button</span><span class="o">;</span> <span class="nt">background</span><span class="nd">:</span> <span class="err">$</span><span class="nt">gray</span><span class="o">;</span><span class="p">}</span>
So why didn't we just use nesting? Extending keeps you from having to write multiple class names on html elements instead of writing it like the following:
<span class="nt"><a</span> <span class="na">class=</span><span class="s">"button primary"</span> <span class="na">href=</span><span class="s">"http://thinkshout.com"</span><span class="nt">></span>ThinkShout<span class="nt"></a></span>
We can use one class because 'primary' will include all the same styles as 'button.'
<span class="nt"><a</span> <span class="na">class=</span><span class="s">"primary"</span> <span class="na">href=</span><span class="s">"http://thinkshout.com"</span><span class="nt">></span>ThinkShout<span class="nt"></a></span>
Using SASS in Your Theme
The stylesheets in your Zen sub-theme are organized according to the principles of SMACSS. You'll notice the style.scss
file doesn't actually contain any styles, but only imports. The _init.scss
file contains additional imports such as Zen Grids and Compass utilities, mixins and helpers. If you look in layouts/responsive.scss
, you'll see the Zen theme includes a mobile-first responsive layout by default.
Let's add some sass of our own. Add a file called _main-nav.scss
to the components
directory. In that file, add the following SASS. It's similar to the style we used in our SASS primer.
<span class="nn">#navigation</span> <span class="p">{</span> <span class="na">background</span><span class="o">:</span> <span class="nv">$sand</span><span class="p">;</span> <span class="nc">.links</span> <span class="p">{</span> <span class="nt">a</span><span class="o">,</span> <span class="nt">a</span><span class="nd">:visited</span> <span class="p">{</span> <span class="na">color</span><span class="o">:</span> <span class="nv">$white</span><span class="p">;</span> <span class="na">text-decoration</span><span class="o">:</span> <span class="no">none</span><span class="p">;</span> <span class="k">&</span><span class="nd">:hover</span> <span class="p">{</span> <span class="na">color</span><span class="o">:</span> <span class="nv">$brown</span><span class="p">;</span> <span class="k">@include</span><span class="nd"> transition</span><span class="p">(</span><span class="no">color</span><span class="o">,</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span><span class="o">,</span> <span class="n">ease-out</span><span class="o">,</span> <span class="mi">0</span><span class="mf">.5</span><span class="kt">s</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span><span class="p">}</span>
In order to get this change to take effect, you need to import it into your style.scss
. Add an import statement for _main-nav.scss
in the components section.
<span class="cm">/* Component (SMACSS module) rules */</span><span class="k">@import</span> <span class="s2">"components/misc"</span><span class="p">;</span><span class="k">@import</span> <span class="s2">"components/main-nav"</span><span class="p">;</span> <span class="c1">// Add this import statement</span>
You may be wondering why you don't need the underscore in front of the file when importing. The underscore tells SASS that the file is a partial. The partial won't be compiled into its own file. It will be included in the style.css
when compiled. If you don't have Compass running, go ahead and run compass watch
in your theme directory or use your toolkit to compile. You should see your navigation style applied to your Drupal site when you refresh.
As you progress in your SASS development, I encourage you to use the SASS Globbing gem. It makes importing a breeze.
Now that you have used SASS in your theme and have the basics down, be sure to check out the SASS and Zen Grids documentation to be even more productive in your theme development. Get the code for this article on Github.