I build pieces of the web at Start Interaction.
I am various permutations of myself on…
I help organize Fox Valley Drupal.
It's slightly different by platform, but essentially boils down to the following command:
gem install sass
Every valid CSS3 stylesheet is valid .scss. You can change the extension of an existing file to get rolling.
From your project directory run:
sass --watch style.scss:style.css
This will compile your .scss file into a .css file.
Use variables to store values you use all over the place.
$black: rgb(30, 30, 30);
$green: rgb(116, 187, 2);
$base-font-size: 16px;
$base-line-height: 20px;
body {
color: $black;
font-size: $base-font-size;
line-height: $base-line-height;
padding: $base-font-size*2;
}
h1 {
color: $green;
}
body {
color: #1e1e1e;
font-size: 16px;
line-height: 20px;
padding: 32px;
}
h1 {
color: #74bb02;
}
Stop repeating yourself and doing things over again.
footer {
border-top: 1px solid $black;
}
footer p {
font-size: $base-font-size*.8;
}
footer p.legal {
float: left;
}
footer p.contact {
float: right;
}
footer {
border-top: 1px solid $black;
p {
font-size: $base-font-size*.8;
a {
color: $green;
}
.legal {
float: left;
}
}
}
Create reusable chunks of CSS properties with @mixin
and apply with @include
.
$sans-serif: "Helvetica Neue", sans-serif;
$serif: Georgia, serif;
@mixin font-body {
font-family: $sans-serif;
font-weight: 400;
}
@mixin font-headers {
font-family: $serif;
font-weight: 400;
}
body {
@include font-body;
}
h1 {
@include font-headers;
}
body {
font-family: "Helvetica Neue", sans-serif;
font-weight: 400;
}
h1 {
font-family: Georgia, serif;
font-weight: 400;
}
One selector can inherit properties from another without duplication using @extend
.
.error {
border: 1px #f00;
background: #fdd;
}
.error.intrusion {
font-size: 1.3em;
font-weight: bold;
}
.badError {
@extend .error;
border-width: 3px;
}
.error, .badError {
border: 1px #f00;
background: #fdd;
}
.error.intrusion,
.badError.intrusion {
font-size: 1.3em;
font-weight: bold;
}
.badError {
border-width: 3px;
}
Instead of a class you can create a set of properties that don't write directly to the CSS unless applied with @extend
.
%message {
border: 1px black;
border-radius: 5px;
font-size: .9em;
}
.error {
@extend %message;
background: red;
}
.success {
@extend %message;
background: green;
}
.error, .success {
border: 2px solid black;
border-radius: 5px;
font-size: .9em;
margin-bottom: 1em;
padding: .5em;
}
.error {
background: red;
}
.success {
background: green;
}
Decide what works best in your project. Mixins can create duplicate CSS
As a rule of thumb, it’s better to never extend a class selector and always to use a placeholder selector in it’s place instead.
Chris Lamb
Compass is a collection of helpful tools and battle-tested best practices for Sass.
It's slightly different by platform, but essentially boils down to the following command:
gem install compass
Install compass and Sass will be installed for you.
http://snugug.com/musings/installing-sass-and-compass-across-all-platform
Compass will create a project directory for you. If you’re creating a new Drupal theme…
cd sites/all/themes
compass create theme_name
This will create a directory called "theme_name" with subdirectories and a config.rb file.
Compass uses its own compiler. So now instead of
sass --watch style.scss:style.css
we now use
compass watch
Compass is now watching for changes and will compile when you save files with changes.
Compass provides a robust set of code patterns for re-use.
Compass writes out CSS3 properties with browser fallbacks.
%message {
@include border-radius(3px);
@include box-shadow();
border: 1px black;
}
.error {
@extend %message;
background: red;
}
.success {
@extend %message;
background: green;
}
.error, .success {
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
-ms-border-radius: 3px;
-o-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 0 2px 10px 2px #323232;
-moz-box-shadow: 0 2px 10px 2px #323232;
box-shadow: 0 2px 10px 2px #323232;
border: 1px black;
margin-bottom: 1em;
}
.error {
background: red;
}
.success {
background: green;
}
Compass ships with a very powerful (and slightly confusing) vertical rhythm system.
@include establish-baseline;
h1 {
@include adjust-font-size-to(39px, 2);
@include rhythm(0, 0, 0, 1, 39px);
}
html {
font-size: 16px;
line-height: 1.25em;
}
h1 {
font-size: 2.4375em;
line-height: 1.02564em;
margin-top: 0em;
padding-top: 0em;
padding-bottom: 0em;
margin-bottom: 0.51282em;
}
Compass has some great utilities for text, lists, image replacement, and more.
nav {
ul {
@include inline-block-list(1em);
}
}
#{headings(1,4)} {
@include font-headers;
a {
@include link-colors(inherit, $hover, $active, inherit, $focus);
}
}
nav ul {
margin: 0;
padding: 0;
border: 0;
}
nav ul:after {
content: "";
display: table;
clear: both;
}
nav ul li {
list-style-image: none;
list-style-type: none;
margin-left: 0;
display: -moz-inline-stack;
display: inline-block;
vertical-align: middle;
*vertical-align: auto;
zoom: 1;
*display: inline;
white-space: nowrap;
padding-left: 1em;
padding-right: 1em;
}
h1, h2, h3, h4 {
font-family: Georgia, serif;
font-weight: 400;
}
h1 a, h2 a, h3 a, h4 a {
color: inherit;
}
h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited {
color: inherit;
}
h1 a:focus, h2 a:focus, h3 a:focus, h4 a:focus {
color: #448087;
}
h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover {
color: #448087;
}
h1 a:active, h2 a:active, h3 a:active, h4 a:active {
color: #141414;
}
Compass extensions allow for the inclusion of stylesheet libraries, templates, patterns, and more.
On the command line
gem install {extension}
In your config.rb
file
require '{extension}'
In your SCSS base
@import '{extension}'
Set breakpoint variables and do amazing things!
// Your basic media queries, min-width and min/max width, are super easy!
$basic: 543px;
$pair: 456px 794px;
#foo {
content: 'No Media Queries';
@include breakpoint($basic) {
content: 'Basic Media Query';
}
@include breakpoint($pair) {
content: 'Paired Media Query';
}
}
A ratio based grid framework that helps you leave 960 in the dust! Works well with Breakpoint too.
$grids: 12;
$grids: add-grid(2 8 2 at $desk);
$gutters: 1/3;
article {
@include breakpoint($desk) {
@include grid-span(8, 3);
}
}
aside {
@include breakpoint($desk) {
@include grid-span(2, 1);
}
}
button {
@include sassy-button("shiny", 15px, 26px, #ffd71a, #ffaa1a);
}
Toolkit bundles a bunch of useful responsive tools into one, well… toolkit.
Yes, there are Drupal modules with these names.
No. I have not used these. I'm not entirely sure why you would want to.
Sass and Compass are authoring tools, not server side components.
Use your favorite search engine to find thousands of articles to help you.
Read this presentation from Sam Richard for more depth on what we covered tonight.