deep dive into css preprocessors

56
CSS Preprocessors @SFHTML5 – May 31st 2012 Deep dive into

Upload: verekia

Post on 27-Jan-2015

135 views

Category:

Technology


0 download

DESCRIPTION

During this talk, Jonathan Verrecchia (@verekia) demonstrates the power of CSS preprocessors and explain why he believes these are a game changer for front-end development. CSS preprocessors are used to enhance CSS with amazing new features (variables, functions, file concatenation and minification, color operations, etc.) and can dramatically improve your development workflow. Jonathan sheds light on some very common misconceptions about them and will help you choose between Sass Less and Stylus for your next project. The talk will cover the basics of CSS preprocessors but also more advanced techniques with concrete use cases. If you haven’t dived into CSS preprocessors yet, if you’re still skeptical, or if you want one more proof that they’re the way to go now, this talk is for you!

TRANSCRIPT

Page 1: Deep Dive Into CSS Preprocessors

CSS Preprocessors

@SFHTML5 – May 31st 2012

Deep dive into

Page 2: Deep Dive Into CSS Preprocessors

Jonathan Verrecchia

Front-End Engineer at

Author of a French book about HTML5

Created H5BP's builder

I'm @verekia and I blog on verekia.com

Page 3: Deep Dive Into CSS Preprocessors

Questions

• Feel free to interrupt me at anytime

• Or tweet your questions using #csspp

?

Page 4: Deep Dive Into CSS Preprocessors

I will talk about

• CSS weaknesses

• Preprocessors features

• Common misconceptions

• Sass, Less or Stylus?

• Workflow and techniques

• Preprocessors + OOCSS

Page 5: Deep Dive Into CSS Preprocessors

CSS weaknesses

Page 6: Deep Dive Into CSS Preprocessors

At the office...

Product Manager:

“Can we try changing our theme quickly? I think blue buttons would be more engaging!”

You:

“Hm... It's not that quick. Come back in 30 min?”

Product Manager 30 min later:

“Ah it's ugly! Can we try a lighter blue and compare it to this orange and green too?”

Page 7: Deep Dive Into CSS Preprocessors

CSS variables?

You won't be able to use them until

IE 7, 8, 9 , 10, 11, [...] usage drops below 1%.

“Variables... finally!”

Page 8: Deep Dive Into CSS Preprocessors

3 major CSS weaknesses

• Lack of essential basic features

• Hard to maintain (huge messy CSS files)

• Not DRY enough - leads to mistakes

Page 9: Deep Dive Into CSS Preprocessors

Working with multiple files

Having multiple CSS files is essential for maintainability and collaboration

But @import sucks (additional HTTP requests)

We need to use a build tool for concatenation.

... but maybe it can do more than just that?

This is what CSS preprocessors do :)

Page 10: Deep Dive Into CSS Preprocessors

The 3 most popular preprocessors

They are all great :)

Page 11: Deep Dive Into CSS Preprocessors

Features

Page 12: Deep Dive Into CSS Preprocessors

About those features...

Less, Sass and Stylus support them all

They don't enhance CSS, but improve your development workflow

Page 13: Deep Dive Into CSS Preprocessors

Variables

They are actually constants (and it's fine)

$my_blue: #4e6cb0; // Sass @my_blue: #4e6cb0; // Less my_blue = #4e6cb0; // Stylus $primary_color: $my_blue; h1 { color: $primary_color; }

Page 14: Deep Dive Into CSS Preprocessors

Variables Types

Variables are not just for colors!

$fonts: Helvetica, Arial, sans-serif; $images_path: "../img/" $font-size: 1.5em; $margin: 20px; $width: 80%;

Page 15: Deep Dive Into CSS Preprocessors

Operations and color functions

Math and colors operations

Color functions

width: 25px * 4 + 100px / 2 – 50px; // = 100px color: #990033 + #666666; // = #FF66CC

$light_blue: lighten($my_blue, 20%); $flashy_blue: saturate($my_blue, 50%);

Page 16: Deep Dive Into CSS Preprocessors

Mixins

Functions like lighten() return a value.

Mixins don't return anything but output CSS.

@mixin border-radius($radius: 10px) { -webkit-border-radius: $radius; -moz-border-radius: $radius; border-radius: $radius; } .button { @include border-radius(4px); }

Page 17: Deep Dive Into CSS Preprocessors

Nesting .pop { .pop-btn { } } // ======== OUTPUT ======== .pop {} .pop .pop-btn {}

.link { &:hover { } } // ======== OUTPUT ======== .link {} .link:hover {}

Don't nest too much (4 levels max)

Page 18: Deep Dive Into CSS Preprocessors

Imports and minification

Imports now work with no performance costs!

CSS output can be clear or minified

@import "colors"; @import "typography"; @import "layout";

#main{color:#fff;background-color:#000}#main p{width:10em}.huge{font-size:10em;font-weight:bold;text-decoration:underline}

Page 19: Deep Dive Into CSS Preprocessors

Misconceptions

Page 20: Deep Dive Into CSS Preprocessors

“I need a Ruby or Node backend”

• Sass is written in Ruby

• Less and Stylus are written in JavaScript

This does NOT mean you need Ruby or Node on your production server to use them.

You need them on your

development machine.

Page 21: Deep Dive Into CSS Preprocessors

“Less runs client-side”

Less can run client-side.

OK for quick testing or to compile in-browser if you can't access your dev environment.

Never - EVER - use it in production, it's even worse than CSS' @import for performance.

Always use Node (or Rhino).

Page 22: Deep Dive Into CSS Preprocessors

“It's hard to install”

Both Ruby and Node are easy to install on any platform:

Ruby is pre-installed, Node.pkg

RubyInstaller.exe, Node.exe

apt-get install ruby / nodejs

Page 23: Deep Dive Into CSS Preprocessors

“Bootstrap uses Less, so it's better”

According to @fat, they chose Less because:

• It was compiling faster than Sass (probably not true anymore since Sass has caching)

• They are friends with Alexis Sellier (Less) so they can easily request a fix or a feature

So nothing you should worry about.

Page 25: Deep Dive Into CSS Preprocessors

Sass, Less or Stylus?

Page 26: Deep Dive Into CSS Preprocessors

How to choose?

Bad criterion

• The syntax (almost the same)

• Twitter Bootstrap (exists in Sass)

Good criterion

• Available features (Sass and Stylus)

• Community (Sass and Less)

• Ruby vs. JS if you want to get involved

Page 27: Deep Dive Into CSS Preprocessors

Related projects

Sass: Hampton Catlin - @hcatlin (Inventor)

Nathan Weizenbaum - @nex3 (Lead developer)

Stylus: TJ Holowaychuk - @tjholowaychuk

Page 28: Deep Dive Into CSS Preprocessors

Lead and (almost) unique developer of Less

Page 29: Deep Dive Into CSS Preprocessors

Choose wisely...

Page 30: Deep Dive Into CSS Preprocessors

Sass welcome package!

• 2 syntaxes: .sass and .scss (CSS-like)

• Twitter Bootstrap and ZURB Foundation

• Even more crazy features (extend, if, for...)

Recommended blog:

Page 31: Deep Dive Into CSS Preprocessors

Compass

Chris Eppstein - @chriseppstein (Sass core)

CSS3, Spriting, Grids, Typography, Data-URLs, Cross-browser, assets path, tons of helpers...

@import "compass/css3/border-radius"; .button { @include border-radius(10px); }

Page 32: Deep Dive Into CSS Preprocessors

sonspring.com

Page 33: Deep Dive Into CSS Preprocessors

Workflow

Page 34: Deep Dive Into CSS Preprocessors

Installing and Running

Just Sass:

Sass + Compass:

> gem install sass > sass --watch <path>

> gem install compass > compass create <path> > compass watch

Page 35: Deep Dive Into CSS Preprocessors

Avoiding the command line

Codekit

Page 36: Deep Dive Into CSS Preprocessors

GUI compilers

Codekit, LiveReload, Less.app, Compass.app, Scout, Crunchapp, SimpLess

Compass.app, Scout, Crunchapp, SimpLess, WinLess, LiveReload (alpha)

Compass.app, Scout, Crunchapp, SimpLess

Page 37: Deep Dive Into CSS Preprocessors

Syntax Highlighting

Editors and IDEs that support Sass:

VIM, Emacs, Sublime Text 2, TextMate, Eclipse, NetBeans, WebStorm, Visual Studio, PyCharm, RadRails, RubyMine, Komodo, Coda, GEdit, [...]

Page 38: Deep Dive Into CSS Preprocessors

Project structure example css/

global.scss

currentpage.scss

scss/

global/

_colors.scss

_helpers.scss

_mixins.scss

_reset.scss

_typography.scss

components/

_buttons.scss

_popups.scss

global.css

currentpage.css

Underscore-prefixed files don't generate .css files

Page 39: Deep Dive Into CSS Preprocessors

Debugging

Debug mode

Firebug plugin: FireSass

> sass –-watch <path> --debug-info

Page 40: Deep Dive Into CSS Preprocessors

Techniques

Page 41: Deep Dive Into CSS Preprocessors

Handy conversions

Pixels to ems

Pixels to percentages

$wrapper_width: 1140px; .responsive-unit { width: percentage(200px / $wrapper_width) margin: percentage(15px / $wrapper_width) }

font-size: (18px / $context) * 1em; // 1.125em

Page 42: Deep Dive Into CSS Preprocessors

Nesting and Media Queries

Sass 3.2 allows blocks as mixins parameters:

.menu-item { display: inline-block; @media screen and (max-width: 480px) { display: block; } }

.menu-item { display: inline-block; @include respond-to(small-screen) { display: block; } }

Page 43: Deep Dive Into CSS Preprocessors

Mobile-first responsive design

@media (min-width: 480px) {}

Page 44: Deep Dive Into CSS Preprocessors

Mobile-first responsive design

Page 45: Deep Dive Into CSS Preprocessors

Mobile-first responsive design

by Nicolas Gallagher ♥

// modern.scss @import "base"; @media (min-width: 480px) { @import "480-up" } @media (min-width: 768px) { @import "768-up" } @media (min-width: 1200px) { @import "1200-up" }

// ie.scss @import "base"; @import "480-up"; @import "768-up";

Page 46: Deep Dive Into CSS Preprocessors

Using nesting with Modernizr and IE

.menu-item { display: inline-block; .ltie8 & { display: inline; zoom: 1; } } // ======== OUTPUT ======== .menu-item {} .ltie8 .menu-item {}

.map { .no-geolocation & { background: "map.png"; } } // ======== OUTPUT ======== .map {} .no-geolocation .map {}

Page 47: Deep Dive Into CSS Preprocessors

Generating a color palette

by Joshua Johnson

$base: #633; $complement1: adjust-hue($base, 180); $complement2: darken(adjust-hue($base, 180), 5%); $lighten1: lighten($base, 15%); $lighten2: lighten($base, 30%);

Page 48: Deep Dive Into CSS Preprocessors

OOCSS

Page 49: Deep Dive Into CSS Preprocessors

OOCSS

OOCSS is a popular solution for large-scale CSS websites by Nicole Sullivan (@stubbornella)

Its core principle is to use multiple classes to make objects inherit from each other

It's heavy for the DOM but very efficient in terms of CSS file size in the long run

<a class="button big-button">

Page 50: Deep Dive Into CSS Preprocessors

Facebook's DOM

MOTHER OF DOM...

Page 51: Deep Dive Into CSS Preprocessors

SASS has @Extend .button { background: #00f; } .big-button { @extend .button; width: 200px; } // ======== OUTPUT ======== .button, .big-button { background: #00f } .big-button { width: 200px }

Page 52: Deep Dive Into CSS Preprocessors

@Extend vs OOCSS

If you build a complex components library with @extend, you'll have a LOT of selectors

• @extend ruins your production CSS

• OOCSS ruins your DOM

My opinion: At some point @extend might overtake OOCSS when we'll have better tools

(we debug seamlessly minified CSS already)

Page 53: Deep Dive Into CSS Preprocessors

Preprocessors + OOCSS

Without @extend, preprocessors and OOCSS are perfectly compatible

Preprocessors are for the stuff you don't want in your production CSS (they help developers)

OOCSS is for the stuff you want in your production CSS (it is performant)

Page 54: Deep Dive Into CSS Preprocessors

Developer Efficiency Chart

Naive CSS

Less Stylus Sass

OOCSS

Less OOCSS

Stylus OOCSS

Sass OOCSS

Parameters: • DRY and file size • Features • Community

Page 55: Deep Dive Into CSS Preprocessors

By becoming web developers, you agreed on staying up to date with all the new cool stuff.

This is the new cool stuff :)