标签:
I‘m learning Less and Sass these two days. Hope I can record something and use them in future.
Disadvantages of CSS
LESS and Sass help you organize your CSS into reuseable objects and classes that help you organize your styles, cut down on code, and make things easier to track.
For Sass,need to install Ruby. Then install sass -- gem install sass. Complie your scss file to css file by it. -- sass --update styles.scss
For LESS, you can use less.js to generate the css file on-the-fly from your own less file. But it depends on the JS and may cause some performance issue when your less file is big. You can downlod a client-side compiler -- SimpLESS, to compile your css file on server side and upload to your web application.
LESS --- .less
using variables in LESS (@)
example
LESS file:
@myColor1: navy; // named color value @myColor2: #000080; // hex color value @myStringVar: " with an appended string"; // string variable @myFontSize: 24px; // numeric value @thinBorder: 4px solid @myColor1; // multi-value variable @paddingVar: 15px 15px 15px 35px; // multi-value variable h1, h2 { color: @myColor1; } #mypara { font-size: @myFontSize; border: @thinBorder; padding: @paddingVar; } #mypara:after { content: @myStringVar; }
Compiled/translated by Simpless:
h1,h2{color:#000080} #mypara{font-size:24px;border:4px solid #000080;padding:15px 15px 15px 35px} #mypara:after{content:" with an appended string"}
using mixins in LESS -- mixins let you define common properties once, then re-use them
.mymixin{ color: #ccc; font-size: 24px; } .myclass{ .mymixin; border: 1px solid black; }
nested ruls in LESS -- it would be helpful to build and maintain the CSS for something like a child selector, with a pseudo class on it.
&: refer to the parent of the rulw that is currently being defined.
body { font-family: Helvetica, Arial, sans-serif; p { font-family:Times; } p#mypara { font-family: "Courier New"; } } #mypara { color: #404040; border: 1px solid #404040; padding: 10px; &:hover { color: red; } }
compiled to
body{font-family:Helvetica,Arial,sans-serif} body p{font-family:Times} body p#mypara{font-family:"Courier New"} #mypara{color:#404040;border:1px solid #404040;padding:10px}
#mypara:hover{color:red}
Operation in LESS --caculations in LESS
@color: #f00; @basepadding: 10px; @basethickness: 1px; #mypara { color: @color + #00f; border: (@basethickness+1)/2 solid black; padding: @basepadding @basepadding + 2; }
compiled:
#mypara { color: #ff00ff; border: 1px solid #000000; padding: 10px 12px; }
标签:
原文地址:http://www.cnblogs.com/underivy/p/4563242.html