CSS Pre-Processors

21st Century CSS

Steve Workman

@steveworkman

Who I am

PA Consulting Group Logo

Weather Observation Website

Say hello to the CSS WG

CSS Working Group 2012
W3C CSS Working Group in 2010 - Photo by Daniel Glazman

A new kind of dependency hell

As a consequence of this design, feature modules couldn't progress unless the core modules progressed, and the core modules couldn't be complete until all the feature modules were complete. Nothing could move forward until everything moved forward. It was a new kind of dependency hell.

Taking a Modular Approach

CSS Modules and snapshots
CSS Modules and snapshots - MDN

There's lots of work going on

Current CSS specification statuses
Current CSS Specification statuses

Calculations (CSS Values)

Example

CSS Variables Cascading Variables

The proposed spec is NOT about variables and I seriously wonder if we should not change the title of the document. You may call the feature it introduces "variables" but at the deeper level, that's not about variables.
Daniel Glazman, CSSWG Co-Chair, August 2012

[CSS Cascading Variables is] a family of custom author-defined properties known collectively as custom properties, which allow an author to assign arbitrary values to a property with an author-chosen name, and variables, which allow an author to then use those values in other properties elsewhere in the document.

First requested in 1998, first W3C proposal: 2008, first public draft April 2012

A decade of BBC News

2001

BBC News 2001

Lines of CSS: 1493
Total File size: 31.85KB (1 file)

2007

BBC News 2007

Lines of CSS: 2270 Up 52%
Total File size: 61.93KB (12 files)

2012

BBC News 2012

Lines of CSS: 19712 Up 860%!
Total File size: 336.30KB (12 files)

How much code?

Graph of the previous slide

How much code?

Graph of the previous slide

Why do we need these things?

http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/00000/0000/500/514/514.strip.gif

Dilbert.com

Enter CSS Pre-processors

Less CSS

SASS

Both feature

  • Nested rules
  • Variables
  • Mixins
  • Logic & Loops
  • Math functions
  • Extensions
  • Works with regular CSS

Nesting Rules

CSS

SASS

Nesting is really useful for @media

Standard CSS

SASS

Variables

$ or @

Who cares?

Neither implementation matches the current W3C spec, so why does it matter?

It doesn't matter, just be aware

$ - Selectors Level 4 Parent Selector

@ - Lots of usage

Mixins

CSS

Less

Mixins contd…

Mixins.less

Clever @importing is the key

Bootstrap.less

Bootstrap's responsive.less

Modular CSS = DRY Code

Re-using CSS has always between projects has always been a challenge, tips and tricks from one project get lost and re-written for another. CSS pre-processors can change that

Going deeper

Extensible classes

Less

Sass

Variable scoping

Less

Sass

Now for the funkier stuff

Loops & Logic

Sass

Generating Rules

Less

CSS

And then Less gets left behind

@content

A mixin include can now accept a block of content. The style block will be passed to the mixin and can be placed at the point @content is used

Sass

CSS

Directive Interpolation

You can now output values of variables into standard CSS directives such a @font-face and @media

This allows for complicated mixins in a much simpler syntax

Even more features in Sass 3.2

Compass

Wait! It's not over

Less 1.3.1 came out 3 days ago

A big (little) update

Is one better than the other?

Yes, but don't think like that

Is one better than the other?

Sass

Sass - a Ferrari 458

Less

Less - a Porsche 911

CSS

CSS - a Lada

When should I use it?

All the time

Get Started

Command line

Less

npm install -g less
lessc style.less style.css

Sass

gem install sass
gem install compass
sass --watch style.scss:style.css

.NET

dotless

Direct port of Less to .NET

PM> Install-Package dotless

Bundle Transformer: Sass and SCSS

Translator adaptor for the basic Ruby library

PM> Install-Package BundleTransformer.SassAndScss

Both allow for .less/.scss files to be read at runtime and bundled & minified for production

Get Started contd...

PHP

Less

lessphp - a compiler for LESS written in PHP

require "lessc.inc.php";
$less = new lessc;
echo $less->compile(".block { padding: 3 + 4px }");

Sass

PHPSass - a compiler for Sass written in PHP, a fork of the older PHamlP

require_once('SassParser.php');
$parser = new SassParser($settings);
$result = $parser->toCss($input);

Get Started contd...

Wordpress

Theme designers can write their CSS in Less or Sass and make use of in-theme bootstrappers so no plugins are required

Less

Use WP-LESS

// wp-content/themes/your-theme/functions.php
require dirname(__FILE__) . ‘/vendor/wp-less/bootstrap-for-theme.php’;
$less = WPLessPlugin::getInstance();
$less->dispatch();

Sass

Use WP-SASS

require_once( 'wp-sass/wp-sass.php' );
// enqueue a .scss style sheet
wp_enqueue_style( 'style', get_stylesheet_directory_uri() . '/style.scss' );

Get Started Contd...

Javascript

Less is written in JavaScript, so you can run it client-side

<link rel="stylesheet/less" href="main.less" type="text/css">
<script src="less.js" type="text/javascript"></script>
So while it's a little more setup to get started, we (the sass core team) think that server side compilation is the best long term approach. Similarly, the less developers prefer server side compilation for production stylesheets.

Get Started - Tools

Grunt
Yeoman
Code Kit

Find out more

Less CSSLesscss.org

SASSsass-lang.com

Stylus
learnboost.github.com/stylus - A third way by @tjholowaychuk

SMACCSsmacss.com - A great e-book on modular CSS by Jonathan Snook

Thank you

Questions?

PA are hiring: www.paconsulting.com/careers/

#