Deep dive intoCSS Preprocessors    @SFHTML5 – May 31st 2012
Jonathan VerrecchiaFront-End Engineer atAuthor of a French book about HTML5Created H5BPs builderIm @verekia and I blog on ...
Questions• Feel free to interrupt me at anytime• Or tweet your questions using #csspp                  ?
I will talk about• CSS weaknesses• Preprocessors features• Common misconceptions• Sass, Less or Stylus?• Workflow and tech...
CSS weaknesses
At the office...Product Manager:“Can we try changing our theme quickly? I thinkblue buttons would be more engaging!”You:“H...
CSS variables?You wont be able to use them untilIE 7, 8, 9 , 10, 11, [...] usage drops below 1%.                    “Varia...
3 major CSS weaknesses• Lack of essential basic features• Hard to maintain (huge messy CSS files)• Not DRY enough - leads ...
Working with multiple filesHaving multiple CSS files is essential formaintainability and collaborationBut @import sucks (a...
The 3 most popular preprocessors       They are all great :)
Features
About those features...Less, Sass and Stylus support them allThey dont enhance CSS, but improve yourdevelopment workflow
VariablesThey are actually constants (and its fine)$my_blue: #4e6cb0; // Sass@my_blue: #4e6cb0; // Less my_blue = #4e6cb0;...
Variables TypesVariables are not just for colors!$fonts: Helvetica, Arial, sans-serif;$images_path: "../img/"$font-size: 1...
Operations and color functionsMath and colors operationswidth: 25px * 4 + 100px / 2 – 50px; // = 100pxcolor: #990033 + #66...
MixinsFunctions like lighten() return a value.Mixins dont return anything but output CSS.@mixin border-radius($radius: 10p...
Nesting.pop {                        .link {    .pop-btn {                    &:hover {    }                             }...
Imports and minificationImports now work with no performance costs!@import "colors";@import "typography";@import "layout";...
Misconceptions
“I need a Ruby or Node backend”• Sass is written in Ruby• Less and Stylus are written in JavaScriptThis does NOT mean you ...
“Less runs client-side”Less can run client-side.OK for quick testing or to compile in-browserif you cant access your dev e...
“Its hard to install”Both Ruby and Node are easy to install on anyplatform:     Ruby is pre-installed, Node.pkg     RubyIn...
“Bootstrap uses Less, so its better”According to @fat, they chose Less because:• It was compiling faster than Sass (probab...
Even big guys can make mistakes
Sass, Less or  Stylus?
How to choose?Bad criterion    • The syntax (almost the same)    • Twitter Bootstrap (exists in Sass)Good criterion   • Av...
Related projectsSass: Hampton Catlin - @hcatlin (Inventor)Nathan Weizenbaum - @nex3 (Lead developer)Stylus: TJ Holowaychuk...
Lead and (almost) unique developer of Less
Choose wisely...
Sass welcome package!• 2 syntaxes: .sass and .scss (CSS-like)• Twitter Bootstrap and ZURB Foundation• Even more crazy feat...
CompassChris Eppstein - @chriseppstein (Sass core)CSS3, Spriting, Grids, Typography, Data-URLs,Cross-browser, assets path,...
sonspring.com
Workflow
Installing and RunningJust Sass:> gem install sass> sass --watch <path>Sass + Compass:> gem install compass> compass creat...
Avoiding the command line         Codekit
GUI compilersCodekit, LiveReload, Less.app,Compass.app, Scout, Crunchapp, SimpLessCompass.app, Scout, Crunchapp,SimpLess, ...
Syntax HighlightingEditors and IDEs that support Sass:VIM, Emacs, Sublime Text 2, TextMate, Eclipse,NetBeans, WebStorm, Vi...
Project structure examplecss/  global.scss                      global.css  currentpage.scss                 currentpage.c...
DebuggingDebug mode> sass –-watch <path> --debug-infoFirebug plugin: FireSass
Techniques
Handy conversionsPixels to emsfont-size: (18px / $context) * 1em; // 1.125emPixels to percentages$wrapper_width: 1140px;.r...
Nesting and Media Queries.menu-item {  display: inline-block;  @media screen and (max-width: 480px) {    display: block;  ...
Mobile-first responsive design@media (min-width: 480px)   {}
Mobile-first responsive design
Mobile-first responsive design// modern.scss@import "base";@media (min-width: 480px) { @import "480-up" }@media (min-width...
Using nesting with Modernizr and IE.map {                         .menu-item {                                 display: in...
Generating a color palette$base: #633;$complement1: adjust-hue($base, 180);$complement2: darken(adjust-hue($base, 180), 5%...
OOCSS
OOCSSOOCSS is a popular solution for large-scale CSSwebsites by Nicole Sullivan (@stubbornella)Its core principle is to us...
Facebooks DOM     MOTHER OF DOM...
SASS has @Extend.button {  background: #00f;}.big-button {  @extend .button;  width: 200px;}// ======== OUTPUT ========.bu...
@Extend vs OOCSSIf you build a complex components librarywith @extend, youll have a LOT of selectors• @extend ruins your p...
Preprocessors + OOCSSWithout @extend, preprocessors and OOCSSare perfectly compatiblePreprocessors are for the stuff you d...
Developer Efficiency ChartParameters:                                   Less Stylus                                       ...
By becoming web developers, you agreed onstaying up to date with all the new cool stuff.This is the new cool stuff :)
Thanks!verekia.com/slides/css-preprocessors            by @verekia
Upcoming SlideShare
Loading in...5
×

Deep Dive Into CSS Preprocessors

94,955

Published on

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!

Published in: Technology
3 Comments
76 Likes
Statistics
Notes
No Downloads
Views
Total Views
94,955
On Slideshare
0
From Embeds
0
Number of Embeds
37
Actions
Shares
0
Downloads
468
Comments
3
Likes
76
Embeds 0
No embeds

No notes for slide

Deep Dive Into CSS Preprocessors

  1. 1. Deep dive intoCSS Preprocessors @SFHTML5 – May 31st 2012
  2. 2. Jonathan VerrecchiaFront-End Engineer atAuthor of a French book about HTML5Created H5BPs builderIm @verekia and I blog on verekia.com
  3. 3. Questions• Feel free to interrupt me at anytime• Or tweet your questions using #csspp ?
  4. 4. I will talk about• CSS weaknesses• Preprocessors features• Common misconceptions• Sass, Less or Stylus?• Workflow and techniques• Preprocessors + OOCSS
  5. 5. CSS weaknesses
  6. 6. At the office...Product Manager:“Can we try changing our theme quickly? I thinkblue buttons would be more engaging!”You:“Hm... Its not that quick. Come back in 30 min?”Product Manager 30 min later:“Ah its ugly! Can we try a lighter blue andcompare it to this orange and green too?”
  7. 7. CSS variables?You wont be able to use them untilIE 7, 8, 9 , 10, 11, [...] usage drops below 1%. “Variables... finally!”
  8. 8. 3 major CSS weaknesses• Lack of essential basic features• Hard to maintain (huge messy CSS files)• Not DRY enough - leads to mistakes
  9. 9. Working with multiple filesHaving multiple CSS files is essential formaintainability and collaborationBut @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 :)
  10. 10. The 3 most popular preprocessors They are all great :)
  11. 11. Features
  12. 12. About those features...Less, Sass and Stylus support them allThey dont enhance CSS, but improve yourdevelopment workflow
  13. 13. VariablesThey are actually constants (and its fine)$my_blue: #4e6cb0; // Sass@my_blue: #4e6cb0; // Less my_blue = #4e6cb0; // Stylus$primary_color: $my_blue;h1 { color: $primary_color;}
  14. 14. Variables TypesVariables are not just for colors!$fonts: Helvetica, Arial, sans-serif;$images_path: "../img/"$font-size: 1.5em;$margin: 20px;$width: 80%;
  15. 15. Operations and color functionsMath and colors operationswidth: 25px * 4 + 100px / 2 – 50px; // = 100pxcolor: #990033 + #666666; // = #FF66CCColor functions$light_blue: lighten($my_blue, 20%);$flashy_blue: saturate($my_blue, 50%);
  16. 16. MixinsFunctions like lighten() return a value.Mixins dont 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);}
  17. 17. Nesting.pop { .link { .pop-btn { &:hover { } }} }// ======== OUTPUT ======== // ======== OUTPUT ========.pop {} .link {}.pop .pop-btn {} .link:hover {} Dont nest too much (4 levels max)
  18. 18. Imports and minificationImports now work with no performance costs!@import "colors";@import "typography";@import "layout";CSS output can be clear or minified#main{color:#fff;background-color:#000}#mainp{width:10em}.huge{font-size:10em;font-weight:bold;text-decoration:underline}
  19. 19. Misconceptions
  20. 20. “I need a Ruby or Node backend”• Sass is written in Ruby• Less and Stylus are written in JavaScriptThis does NOT mean you need Ruby or Nodeon your production server to use them.You need them on yourdevelopment machine.
  21. 21. “Less runs client-side”Less can run client-side.OK for quick testing or to compile in-browserif you cant access your dev environment.Never - EVER - use it in production, its evenworse than CSS @import for performance.Always use Node (or Rhino).
  22. 22. “Its hard to install”Both Ruby and Node are easy to install on anyplatform: Ruby is pre-installed, Node.pkg RubyInstaller.exe, Node.exe apt-get install ruby / nodejs
  23. 23. “Bootstrap uses Less, so its 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 featureSo nothing you should worry about.
  24. 24. Even big guys can make mistakes
  25. 25. Sass, Less or Stylus?
  26. 26. 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
  27. 27. Related projectsSass: Hampton Catlin - @hcatlin (Inventor)Nathan Weizenbaum - @nex3 (Lead developer)Stylus: TJ Holowaychuk - @tjholowaychuk
  28. 28. Lead and (almost) unique developer of Less
  29. 29. Choose wisely...
  30. 30. Sass welcome package!• 2 syntaxes: .sass and .scss (CSS-like)• Twitter Bootstrap and ZURB Foundation• Even more crazy features (extend, if, for...)Recommended blog:
  31. 31. CompassChris 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);}
  32. 32. sonspring.com
  33. 33. Workflow
  34. 34. Installing and RunningJust Sass:> gem install sass> sass --watch <path>Sass + Compass:> gem install compass> compass create <path>> compass watch
  35. 35. Avoiding the command line Codekit
  36. 36. GUI compilersCodekit, LiveReload, Less.app,Compass.app, Scout, Crunchapp, SimpLessCompass.app, Scout, Crunchapp,SimpLess, WinLess, LiveReload (alpha)Compass.app, Scout, Crunchapp, SimpLess
  37. 37. Syntax HighlightingEditors and IDEs that support Sass:VIM, Emacs, Sublime Text 2, TextMate, Eclipse,NetBeans, WebStorm, Visual Studio, PyCharm,RadRails, RubyMine, Komodo, Coda, GEdit, [...]
  38. 38. Project structure examplecss/ global.scss global.css currentpage.scss currentpage.css scss/ global/ _colors.scss _helpers.scss Underscore-prefixed files _mixins.scss dont generate .css files _reset.scss _typography.scss components/ _buttons.scss _popups.scss
  39. 39. DebuggingDebug mode> sass –-watch <path> --debug-infoFirebug plugin: FireSass
  40. 40. Techniques
  41. 41. Handy conversionsPixels to emsfont-size: (18px / $context) * 1em; // 1.125emPixels to percentages$wrapper_width: 1140px;.responsive-unit { width: percentage(200px / $wrapper_width) margin: percentage(15px / $wrapper_width)}
  42. 42. Nesting and Media Queries.menu-item { display: inline-block; @media screen and (max-width: 480px) { display: block; }}Sass 3.2 allows blocks as mixins parameters:.menu-item { display: inline-block; @include respond-to(small-screen) { display: block; }}
  43. 43. Mobile-first responsive design@media (min-width: 480px) {}
  44. 44. Mobile-first responsive design
  45. 45. Mobile-first responsive design// 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"; by Nicolas Gallagher ♥
  46. 46. Using nesting with Modernizr and IE.map { .menu-item { display: inline-block; .no-geolocation & { background: "map.png"; .ltie8 & { } display: inline;} zoom: 1; } }// ======== OUTPUT ======== // ======== OUTPUT ========.map {} .menu-item {}.no-geolocation .map {} .ltie8 .menu-item {}
  47. 47. Generating a color palette$base: #633;$complement1: adjust-hue($base, 180);$complement2: darken(adjust-hue($base, 180), 5%);$lighten1: lighten($base, 15%);$lighten2: lighten($base, 30%); by Joshua Johnson
  48. 48. OOCSS
  49. 49. OOCSSOOCSS is a popular solution for large-scale CSSwebsites by Nicole Sullivan (@stubbornella)Its core principle is to use multiple classes tomake objects inherit from each other<a class="button big-button">Its heavy for the DOM but very efficient interms of CSS file size in the long run
  50. 50. Facebooks DOM MOTHER OF DOM...
  51. 51. SASS has @Extend.button { background: #00f;}.big-button { @extend .button; width: 200px;}// ======== OUTPUT ========.button,.big-button { background: #00f }.big-button { width: 200px }
  52. 52. @Extend vs OOCSSIf you build a complex components librarywith @extend, youll have a LOT of selectors• @extend ruins your production CSS• OOCSS ruins your DOMMy opinion: At some point @extend mightovertake OOCSS when well have better tools(we debug seamlessly minified CSS already)
  53. 53. Preprocessors + OOCSSWithout @extend, preprocessors and OOCSSare perfectly compatiblePreprocessors are for the stuff you dont wantin your production CSS (they help developers)OOCSS is for the stuff you want in yourproduction CSS (it is performant)
  54. 54. Developer Efficiency ChartParameters: Less Stylus Sass• DRY and file size OOCSS OOCSS OOCSS• Features• Community OOCSS Sass Less Stylus Naive CSS
  55. 55. By becoming web developers, you agreed onstaying up to date with all the new cool stuff.This is the new cool stuff :)
  56. 56. Thanks!verekia.com/slides/css-preprocessors by @verekia
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×