2. Who is the guy?
· Sven Wolfermann (36)
· Freelancer for modern frontend
development (RWD, HTML5, CSS3)
from Berlin
· CSS3 Adventskalender 2010/2011
· wrotes articles for T3N, PHP-Magazin
and Webstandards-Magazin
(new: Screengui.de)
· Certified TYPO3 Integrator
· Twitter: @maddesigns
· Web: http://maddesigns.de
5. What is Sass?
Sass means syntactically awesome style sheets
is a preprocessor
Similar preprocessors: LESS, Stylus (needs JS, i.e. a node.js server
6. Installing Sass
In order to install and run Sass, you need to have Ruby installed on your system.
Mac OSX
Easy! Ruby is built in :)
Linux
if not installed, use the package manager
$ sudo apt-get install ruby1.9.1-full
on Windows?
use http://rubyinstaller.org/ to install ruby
10. Sass or SCSS?
Sass has two syntaxes. The new main syntax is known as “SCSS” (for
“Sassy CSS”). SCSS files use the extension .scss.
The second, older syntax is known as the indented syntax (or just
“Sass”). Instead of brackets and semicolons, it uses the indentation of
lines to specify blocks. Files in the indented syntax use the extension
.sass.
13. GUIs
many GUIs for compiling
· Livereload ($9.99)
· Fire.App ($14)
· Compass.app ($10)
· CodeKit ($28)
· Prepros ($19)
· Scout App (free)
and build in in many text editors or IDEs
18. Debugging in Sass 3.3
Sourcemaps to work with original files in developer tools
scss --sourcemap sass/styles.scss public/styles.css
Include in Sass:
/*# sourceMappingURL=styles.css.map */
Sassmaps output
{
"version": "3",
"mappings": "4DAUA,qFAWQ,CACJ,OAAO,CAAE,KAAK,CAOlB,…",
"sources": ["../sass/_vars.css”,”../sass/styles.scss"],
"file": "styles.css"
}
24. Nesting
writing long selectors is time consuming
short selectors are better in general
CSS
nav
nav
nav
nav
nav
{float: right;}
li {float: left;}
li a {color: #666;}
li a:hover {color: #333;}
li.current {font-weight: bold;}
26. Nesting
identation with SCSS makes no difference in CSS output
SCSS
nav {float: right;
li {float: left;
a {color: #666;
&:hover {
color: #333;}
}
&.current {
font-weight: bold;}
}}
but sure it looks better if intended
SCSS
27. Nesting
HOW DEEP CAN I GO?
Sass nesting != HTML nesting
be careful with nesting!
you can run into performance issues with long selectors
29. Combining Selectors
div {
color: black
.foo {
color: black } //
+ .foo {
color: black } //
//
> .foo {
color: black } //
~ .foo {
color: black } //
//
& .foo {
color: black } //
//
&.bar {
color: black }
&:hover {
color: black }
}
SCSS
descendant
adjacent
sibling
child
general
sibling
Sass' parent
selector
div {
color: black; }
div .foo {
color: black; }
div + .foo {
color: black; }
div > .foo {
color: black; }
div ~ .foo {
color: black; }
div .foo {
color: black; }
div.bar {
color: black; }
div:hover {
color: black; }
30. Parent Selector - the ampersand
the & (ampersand) has a placeholder function for the parental selector
div {
.foo {
color: black
}
&.foo {
color: black
}
}
div .foo {
color: black
}
div.foo {
color: black;
}
SCSS
31. Parent Selector - the ampersand
a {
&:hover,
&:focus {
color: black
}
}
a:hover, a:focus {
color: black;
}
SCSS
32. Parent Selector - the ampersand
Usecase for Modernizr classes
div {
box-shadow: 0 0 5px rgba(#000, 0.8);
// Sass feature for Hex to RGB colors
.no-boxshadow & {
border: 1px solid #555;
}
}
div {
box-shadow: 0 0 5px rgba(#000, 0.8); }
.no-boxshadow div {
border: 1px solid #555; }
SCSS
33. Parent Selector - the ampersand
div {
.parent & .child {
color: black
}
}
.parent div .child {
color: black;
}
SCSS
34. Parent Selector - the ampersand
@media queries in place
aside {
width: 100%;
@media screen and (min-width: 680px) {
width: 25%;
}
}
aside {
width: 100%;
}
@media screen and (min-width: 680px) {
aside {
width: 25%;
}
}
SCSS
35. BTW did you recognize the Comments?
/* Hey look at this multiline comment
* that we want to show up in our CSS output. */
.container {
color: black; }
// These comments are single lines and
// we do not want them to appear in our CSS
footer {
color: #336699; }
This compiles to:
/* Hey look at this multiline comment
* that we want to show up in our CSS output. */
.container {
color: black; }
footer {
color: #336699; }
SCSS
37. Importing
the way in CSS
/* style.css */
@import "base.css";
@import url("styles.css");
@import url("druck.css") print;
Importing CSS files into one file can cause performance issues
Limit your external references in your HTML
Importing in Sass is better
split your stylesheet in many chunks and use the import function of
Sass
42. @extend
@extend clones the attributes from rules and adds them to another
rule.
.button {
background-color: $color-main;
font-weight: bold;
color: white;
padding: 5px;
}
SCSS
Then we can @extend the class to another
.button-checkout {
@extend .button;
background-color: darken($color-main, 20%);
}
SCSS
44. Placeholder Selectors: %foo
// This ruleset won't be rendered on its own.
%button {
color: blue;
font-weight: bold;
font-size: 2em; }
SCSS
placeholder selectors can be extended, just like classes and IDs. The
extended selectors will be generated, but the base placeholder selector
will not
.btn-notice { @extend %button; }
.btn-notice {
color: blue;
font-weight: bold;
font-size: 2em; }
SCSS
48. Mixins
compiled to
.box {
color: blue;
font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
but thats a bad example – no need for the vendor prefixes of borderradius anymore
only use border-radius: 5px; in your stylesheets
49. Mixins
Defaults and named arguments
@mixin link-colors($text:blue, $hover:red) {
color: $text;
&:hover { color: $hover; }
}
a {
@include link-colors($hover: green);
}
a { color: blue; }
a:hover { color: green; }
SCSS
56. Functions
User Functions
@function grid-width($cells) {
@return ($cell-width + $cell-padding) * $cells;
}
SCSS
Function to calculate the em font size from pixels
@function px2em($font_size, $base_font_size: 16) {
@return $font_size / $base_font_size + em
}
SCSS
58. Variables in queries
use main breakpoints as variables
$break-small: 320px;
$break-large: 1200px;
.profile-pic {
float: left;
width: 100px;
@media screen and (min-width: $break-small) {
width: 250px;
float: none;
}
@media screen and (min-width: $break-large) {
float: right;
}
}
Responsive Web Design in Sass: Using Media Queries in Sass 3.2
SCSS
59. Mixin for different media queries using @content
SCSS
$break-small: 320px;
$break-large: 1200px;
@mixin respond-to($media) {
@if $media == smartpones {
@media only screen and (max-width:
@content;
}
} @else if $media == tablet {
@media only screen and (min-width:
and (max-width:
@content;
}
} @else if $media == desktop {
@media only screen and (min-width:
@content;
}
}
}
$break-small) {
$break-small + 1)
$break-large - 1) {
$break-large) {
60. Mixin for different media queries using @content
Usage
// profile picture module
.profile-pic {
float: left;
width: 250px;
@include respond-to(smartpones) { width: 100% ;}
@include respond-to(tablet) { width: 125px; }
@include respond-to(desktop) { float: none; }
}
SCSS
61. Mixin for different media queries using @content
CSS output sample
.profile-pic {
float: left;
width: 250px;
}
@media only screen and (max-width: 320px) {
.profile-pic {
width: 100%;
}
}
@media only screen and (min-width: 321px) and (max-width: 1199px) {
.profile-pic {
width: 125px;
}
}
@media only screen and (min-width: 1200px) {
.profile-pic {
float: none;
}
}
63. Sass-IE
Writing mobile-first styles without leaving IE < 9 behind
Media Query Mixin:
// all.scss
$fix-mqs: false !default;
@mixin respond-min($width) {
// If we're outputting for a fixed media query set...
@if $fix-mqs {
// ...and if we should apply these rules...
@if $fix-mqs >= $width {
// ...output the content the user gave us.
@content;
}
}
@else {
// Otherwise, output it using a regular media query
@media screen and (min-width: $width) {
@content;
}
}
}
// and a respond-max mixin, that does what you might expect
Sass-IE
64. Sass-IE
OldIE Mixin:
// all.scss
$old-ie: false !default;
@mixin old-ie {
// Only use this content if we're dealing with old IE
@if $old-ie {
@content;
}
}
Separate IE stylesheet
// all-old-oldie.scss
$old-ie: true;
$fix-mqs: 65em;
@import 'all';
65. Including the Sass-IE CSS
To give the CSS to the browsers, use good old conditional comments:
<!--[if lte IE 8]>
<link rel="stylesheet" href="css/all-old-ie.css">
<![endif]-->
<!--[if gt IE 8]><!-->
<link rel="stylesheet" href="css/all.css">
<!--<![endif]-->
67. Compass
Compass is to Sass like jQuery to Javascript
Compass is a Framework, that extends Sass
It brings a lot of CSS3 mixins and useful CSS stuff
http://sonspring.com/journal/sass-for-designers
68. Compass Plugins
Github List of Compass Plugins
loading Compass plugins
add to the config.rb
# config.rb
...
require 'compassplugin'
@import 'compassplugin';
SCSS
69. RGBAPNG Plugin
Compass plugin for providing cross-browser compatible RGBA support
by creating transparent PNGs on the fly for browsers that don't support
RGBA.
https://github.com/aaronrussell/compass-rgbapng
$ sudo gem install compass-rgbapng
@import "rgbapng";
.box {
@include rgba-background(rgba(0,0,0,0.75));
}
.box {
background: url('/images/rgbapng/000000bf.png?1282127952');
background: rgba(0, 0, 0, 0.75); }
SCSS
71. Susy Usage
SCSS
@import "susy";
// global variables
$total-columns: 12;
$column-width: 4em;
$gutter-width: 1em;
$grid-padding: $gutter-width;
//
//
//
//
// usage
.page {
@include container;
@include susy-grid-background;
}
a 12-column grid
each column is 4em wide
1em gutters between columns
grid-padding equal to gutters
72. Susy Usage
.page {
// page acts as a container for our grid.
@include container;
// header and footer are full-width by default.
header, footer { clear: both; }
// nav spans 3 columns of total 12.
nav { @include span-columns(3,12); }
.content {
// content spans the final (omega) 9 columns of 12.
@include span-columns(9 omega,12);
// main content spans 6 of those 9 columns.
.main { @include span-columns(6,9); }
// secondary content spans the final 3 (omega) of 9 columns.
.secondary { @include span-columns(3 omega,9); }
}
}