110 likes | 243 Vues
and. CSS on Steroids. Dr David Stearns Autumn 2013. What are SASS and LESS?. Extensions to the CSS language to support large-scale, complex projects. Tools that compile those extensions into CSS the browser can actually read. LESS file that uses various language extensions.
E N D
and CSS on Steroids Dr David Stearns Autumn 2013
What are SASS and LESS? Extensions to the CSS language to support large-scale, complex projects Tools that compile those extensions into CSS the browser can actually read
LESS file that uses various language extensions Standard CSS file that browsers can understand styles.less LESS compiler styles.css output input
What’s the Difference? Language extensions are nearly identical, with a few specialized features here and there SASS is built on Ruby; LESS is built on JavaScript Both require a compile operation, but LESS has support for compiling in the browser
Language Extensions Variables for values used in multiple places LESS (SASS uses $ instead of @) Compiled CSS @accent-color: #FF7F40; @shading-color: #FFB873; h1 { color: @accent-color; } footer { background-color: @shading-color; } h1 { color: #FF7F40; } footer { background-color: #FFB873; }
Language Extensions Mix-Ins for sets of related properties LESS (SASS uses slightly different syntax) Compiled CSS .trans(@dur: 1s) { -webkit-transition: all @dur; -moz-transition: all @dur; -o-transition: all @dur; -ms-transition: all @dur; transition: all @dur; } nav a { .trans; } .thumbs img { .trans(0.5s); } nav a { -webkit-transition: all 1s; -moz-transition: all 1s; -o-transition: all 1s; -ms-transition: all 1s; transition: all 1s; } .thumbs img { -webkit-transition: all 0.5s; -moz-transition: all 0.5s; -o-transition: all 0.5s; -ms-transition: all 0.5s; transition: all 0.5s; }
Language Extensions Nested Rules SASS and LESS (same syntax) Compiled CSS nav { background-color: #CCC; li { display: inline-block; } a { display: inline-block; padding: 1em; } } nav { background-color: #CCC; } nav li { display: inline-block; } nav a { display: inline-block; padding: 1em; }
Language Extensions Rule Inheritance LESS (SASS uses slightly different syntax) Compiled CSS .headings { font-family: Arial, sans-serif; font-weight: bold; color: #0E0E0E; margin-bottom: 0.5em; } h1:extend(.headings) { font-size: 200%; } h2:extend(.headings) { font-size: 150%; } h1, h2 { font-family: Arial, sans-serif; font-weight: bold; color: #0E0E0E; margin-bottom: 0.5em; } h1 { font-size: 200%; } h2 { font-size: 150%; }
Language Extensions Functions and Expressions LESS (SASS has Similar Functions via Compass) @text-color: #0E0E0E; @shade-color: #FFE873; h1 { color: lighten(@text-color, 10%); } footer { background-color: tint(@shade-color, 50%); } footer:hover { background-color: @shade-color; }
LESS Usage Install LESS using Package Manager that comes with Node.js: npm install -g less During development, use less.js to compile LESS files to CSS on the client: <link rel=“stylesheet/less” href=“less/styles.less”> <script src=“lib/less.js”> For production, use command-line compiler to make final CSS file and link to that: lessc less/styles.less > css/styles.css
SASS Usage Install SASS using Ruby: sudo gem install sass Use command-line compiler to make CSS file: sass sass/styles.scsscss/styles.css Use “Watch Mode” to automatically re-compile whenever .scss file changes sass --watch sass/styles.scss:css/styles.css