This is a presentation I gave on Sept. 25, 2012 for the Winnipeg PHP Group on some of the features in LESS I have started using in my own development environment.
WordPress Websites for Engineers: Elevate Your Brand
Doing More With Less
1. Doing More With
How a meta-language can help you
take control of your stylesheets and
help improve your style productivity.
2. David Engel
• UofM grad
• .NET and PHP developer
• Tipping Canoe alumnus
• Experienced “backend” developer
PHP, C#, C++, MySQL, TransactSQL, Sphinx
• Relatively new to “frontend” development
CSS, jQuery, LESS, UX, design
3. CSS
Cascading Style Sheets
• describes look & formatting of a document
that has been created using markup
• separate style and content
• avoid specificity by harnessing the cascade
4. OOCSS
Object Oriented CSS
• separate structure and skin
• separate container and content
8. DRY CSS
Don’t Repeat Yourself (Jeremy Clarke)
1. Group reusable CSS properties together
2. Use logical names for groups
3. Add selectors (not a fan of ALL-CAPS)
#WHITE-BACKGROUND,
.large-white-background,
.medium-white-background
{
background-color: #fff;
border-color: #bbb;
}
9. DRY CSS
What are we still missing?
• Descendent selectors break OOCSS – so what?
(the “OO” part of OOCSS is garbage anyways)
• No variables
• No mixins
• No inheritance
23. LESS: Guarded mixins
• uses “when” and can use “and” “,” and “not”
• can also use the keywords “true” and “false”
.back-white{background-color:white;}
.back-black{background-color:black;}
.back-red{background-color:red;}
.mixin (@a) when (@a > 10), (@a < -10){
.back-white;}
.mixin (@a) when (@a = 10) and (@a = true){
.back-red;}
.mixin (@a) when (@a < 11) and (@a > -11) and not(@a = 10){
.back-black;}
24. LESS: Guarded mixins
• Comparitors can be very useful
• Eg. Code reuse between media types
@media: mobile;
.mixin (@a) when (@media = mobile) { ... }
.mixin (@a) when (@media = desktop) { ... }
.mixin (@a) when (@media = print) { ... }
26. LESS: Unit checking
• ispixel • ispercentage • isem
.mixin (@a) when (ispixel(@a)) {}
.mixin (@a) when (isem(@a)) {}
.mixin (@a) when (ispercentage(@a)) {}
27. LESS: Colour Functions
lighten(@color, 10%); // return a color which is 10% *lighter* than @color
darken(@color, 10%); // return a color which is 10% *darker* than @color
saturate(@color, 10%); // return a color 10% *more* saturated than @color
desaturate(@color, 10%); // return a color 10% *less* saturated than @color
fadein(@color, 10%); // return a color 10% *less* transparent than @color
fadeout(@color, 10%); // return a color 10% *more* transparent than @color
fade(@color, 50%); // return @color with 50% transparency
spin(@color, 10); // return a color with a 10 degree larger in hue than @color
spin(@color, -10); // return a color with a 10 degree smaller hue than @color
mix(@color1, @color2); // return a mix of @color1 and @color2
30. LESS: Embedding Strings
• For those of you familiar with XSLT or Ruby
• Use curly braces to embed the text
• Use tilde ~ to escape strings that require quotes
@base-url: "http://www.maximinc.com/";
@green-color: #8dc63f;
#header{
background: @ green-color url("@{base-url}images/img.png");
}
#footer{
filter: ~"alpha(opacity = (@{opacityPercentage}))";
}
31. LESS: Javascripting!!!
• Use backticks to embed your JS in the CSS
• Escape strings, embed, interpolate, etc.
• I haven’t played with this feature yet, but here’s an
untested example:
@var1: `"LESS ".toLowerCase()`;
@var2: `"ooooooh".toUpperCase() + '!'`;
@var3: ~`"@{var1: }" + "@{var2: }" + '!'`;
33. LESS: Precompilers
LESS needs to be pre-compiled into CSS using…
• simpless • less.app
• lessphp • …others
OR
to compile on the fly while developing use…
<link rel="stylesheet/less" type="text/css" href="styles.less">
<script src="http://lesscss.googlecode.com/files/less-1.0.30.min.js"></script>
34. LESS: SIMPLESS
• SimpLESS compiles less into minified css
• works with Twitter bootstrap
• self-updates
• compiles on the fly (monitored polling)
• multi-platform
• free
• open source
35. LESS: SIMPLESS
This is what I do…
1. I code my LESSS in a separate project
2. I let SimpLESS monitor the local file to
compile CSS on the fly
3. I commit minified CSS to my main project
4. I reference the minified CSS using with the
SVN version tagged on in my html
<link media="screen" href=http://www.example.com/css/style.min.css?1994
type="text/css" rel="stylesheet“>
36. LESS: Eclipse plugin
I personally use the Xtext plugin – here are some
install instructions…
http://www.normalesup.org/~simonet/soft/ow/eclipse-less.en.html
You can use the LESS compiler in
Eclipse, however I manage all my compilation
externally as I also use LESS with .NET and
Classic ASP code (yes!)
37. Useful links
LESS
http://www.lesscss.org
SimpLESS
http://wearekiss.com/simpless
LessElements
http://lesselements.com
Winnipeg PHP
http://winnipegphp.com
Tipping Canoe
http://www.tippingcanoe.com
David Engel
davidengel.dev@gmail.com
(developed using LESS)
http://www.maximinc.com