On Github taupecat / css-wp
(Follow along at http://www.taupecat.com/csswp/)
There is nothing you can do in a CSS preprocessor that you can't do in pure CSS.
They just make it easier, faster and more organized.
JavaScript-based.
Low barrier to entry because you just include a JavaScript file with your code.
Also works well with node.js.
Ruby-based
Has 2 syntaxes: Sass (indentation-based) and SCSS (more like regular CSS)
Has Compass - framework that adds additional utilities like vendor prefixing, sprite generation, URL-shortcuts.
*Not all features available in all preprocessors.
.sidebar { border-left: 1px solid #262626; padding-left: 20px; aside { margin-bottom: 20px; a { color: #ffe44b; &:hover, &:active, &:focus { color: #e10000; } } } }
.sidebar { border-left: 1px solid #262626; padding-left: 20px; } .sidebar aside { margin-bottom: 20px; } .sidebar aside a { color: #ffe44b; } .sidebar aside a:hover, .sidebar aside a:active, .sidebar aside a:focus { color: #e10000; }
Any string can be a variable.
Great for defining colors and font stacks.
$color-primary: #002366; $font-header: Roboto, "Helvetica Neue", Arial, sans-serif; body { background-color: $color-primary; } h1, h2, h3 { font-family: $font-header; }
body { background-color: #002366; } h1, h2, h3 { font-family: Roboto, "Helvetica Neue", Arial, sans-serif; }
%widget-default { background-color: #FFFCEB; color: #262626; text-decoration: underline; } .sidebar { @extend %widget-default; font-size: 13px; } .widget-area { @extend %widget-default; font-size: 16px; }
.sidebar, .widget-area { background-color: #FFFCEB; color: #262626; text-decoration: underline; } .sidebar { font-size: 13px; } .widget-area { font-size: 16px; }
Two big words for making things smaller.
Sass has "partials", imported files that are concatenated into one bigger file and not processed separately.
-rw-r--r-- 1 taupecat staff 153 May 24 23:36 _asides.scss -rw-r--r-- 1 taupecat staff 130 May 24 23:37 _comments.scss -rw-r--r-- 1 taupecat staff 300 May 24 23:35 _content.scss -rw-r--r-- 1 taupecat staff 5904 May 26 20:55 _global.scss -rw-r--r-- 1 taupecat staff 298 May 24 23:37 _navigation.scss -rw-r--r-- 1 taupecat staff 1731 May 25 22:02 _reset.scss -rw-r--r-- 1 taupecat staff 1731 May 25 22:02 project.scss
@import "reset"; @import "global"; @import "navigation"; @import "content"; @import "asides"; @import "comments";
Resulting compiled "project.css" contains all of the imported files.
No extra HTTP requests required.
Do one thing, then return a result.
Calculate a value or modify a color, for example.
Awesome for responsive design, when you need:
/* target / context = result */ @function calc-percent($target, $context) { @return ($target / $context) * 100%; } @function cp($target, $context) { @return calc-percent($target, $context); } .main-content { width: cp(600px, 960px); }
.main-content { width: 62.5%; }
$default-font-size: 16px !default; @mixin rem($property, $value, $context: $default-font-size) { #{$property}: $value; #{$property}: ($value / $context * 1rem); } h1 { @include rem(font-size, 18px); }
h1 { font-size: 18px; font-size: 1.125rem; }
This is the awesome sauce that makes responsive web design soooo much easier.
.main-content { float: left; margin-right: cp(20px, 960px); width: cp(600px, 960px); @media only all and (max-width: 560px) { float: none; margin-right: 0; width: 100%; } } .sidebar { float: left; width: cp(340px, 960px); @media only all and (max-width: 560px) { float: none; width: 100%; } }
Note: Just an illustration. There are actually better ways to do this.
.main-content { float: left; margin-right: 2.083333333333%; width: 62.5%; } @media only all and (max-width: 560px) { .main-content { float: none; margin-right: 0; width: 100%; } } .sidebar { float: left; width: 35.416666666667%; } @media only all and (max-width: 560px) { .sidebar { float: none; width: 100%; } }
There are even more advanced features that I won't go into detail here:
*Sucking is a highly subjective term.
Use features such as nesting responsibly. (Inception rule)
A few extra characters here & there isn't going to be a big deal so long as you're minifying and Gzipping.
No, it's not exactly CSS, but it's close.
Didn't your mother ever tell you that learning was a lifelong process?
Using Sass?
Use sass --watch style.scss:style.css
Using Compass?
Use compass watch
Compass' config.rb file tells compass where to watch and where to compile to.
If the command line isn't your friend, try a GUI tool.
CodeKit is popular on the Mac.
Handles Sass, Compass, LESS and Stylus.
But wait, there's more!
Just does Sass & Compass, but is cross-platform (Mac/Windows)
Hey, this talk was supposed to be about CSS preprocessors and WordPress!
There are a bunch of plugins that will process your LESS or Sass files and convert them into CSS for WordPress.
Create your *.less file, then add it to your theme using wp_enqueue_style. WP-LESS does the rest.
Creates new function, wpsass_define_stylesheet('stylesheet-name.scss')
For reasons I will demonstrate in a moment, you cannot give your Sass file the name style.scss
Jetpack's CSS editor will recognize and process LESS and Sass (in the SCSS syntax)
Don't name your project's main Sass file "style.scss".
/* Theme Name: Twenty Twelve Theme URI: http://wordpress.org/themes/twentytwelve Author: the WordPress team Author URI: http://wordpress.org/ */
If you're going to minify the final result, those comments go away and your theme will be unusable.*
There's nothing in the WordPress handbook that says you must use style.css as your stylesheet.
*There is a way to force comments to be preserved, even when compressing the output:
/*! Theme Name: Twenty Twelve Theme URI: http://wordpress.org/themes/twentytwelve Author: the WordPress team Author URI: http://wordpress.org/ */
When wouldn't you use CSS preprocessors?
Uh, I dunno.
Responsive WordPress Theming due out this fall.
Or available in early, advanced form NOW. http://www.manning.com/rotton/