今さらネタだったのですが、まあまあ外部リンクがあったので、一応、復活した記事です。
LESSは「ネストとかミックスインとか変数とか使ってCSSを書けるから色々便利です」的なもの(謎)で、Codaのプラグインがあったりもします。
オリジナルが書かれたのは「Feb 13, 2010」みたいです
参考サイトなど
概要
使い方は参考サイトをご覧ください。
- 「レス? リーナーCSSだし、リース?」といきなり名前がわからな。。。
- Sass(Syntactically Awesome Stylesheets)と似てます。 SaaS(Software as a Service)ではない
- LESSをCSSにコンパイルするのにgemが必要
- rubyやruby-gemsのある環境で
- Macだとアプリを使って簡単にコンパイル出来ます
- 変数、ネスト、ミックスイン、演算が使えます
特徴
ネストできる
#header {
width: 960px;
overflow: hidden;
navigation {
float: right;
width: 698px;
overlow: hidden;
li {
float: left;
width: 116px;
a {
display: block;
width: 116px;
height: 24px;
}
}
}
.logo {
float: left;
width: 214px;
}
}
とか書いたものをコンパイルすると
#header {
width: 960px;
overflow: hidden;
}
#header .navigation {
float: right;
width: 698px;
overlow: hidden;
}
#header .navigation li {
float: left;
width: 116px;
}
#header .navigation li a {
display: block;
width: 116px;
height: 24px;
}
#header .logo {
float: left;
width: 214px;
}
とか、してくれます。
ミックスイン出来る
これは、ちょっと内容を変えながら、繰り返し使うようなスタイルを、classなどにまとめておいて、プロパティのように使えちゃう機能です。
こんな感じで、角丸とか作ったものを
/* こんな感じで、角丸とかを用意して */
.round_corner {
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}
/* こんな風に持ってきて */
.button {
background:#a1a1a1;
border:1px solid #ccc;
.round_corner; /* 先に作っておいた角丸のclass */
}
上記の様に書いて展開すると......
.button {
background:#a1a1a1;
border:1px solid #cccccc; /* 省略してたトコは4桁になるみたい */
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}
変数を使える
これも、そのままですが......
/* 変数は「@」で */
@red: #c00;
@blue: #069;
.border {
border: 1px solid @blue;
}
.error .border {
border: 1px solid @red;
}
とか書いたのが......
.border {
border: 1px solid #cc0000;
}
.error .border {
border: 1px solid #006699;
}
といった感じ。
で、こんな使い方も出来ます。
.round_corner (@radius:5px) {
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
border-radius: @radius;
}
.primary_button{
background:#069;
border:1px solid #ccc;
.round_corner(10px);/* 持ってきたスタイルを一部変えたり */
}
とか書いたのが......
.primary_button{
background:#006699;
border:1px solid #cccccc;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
演算が出来ます
まあ、そのままです。
/* 変数 */
@gutter_width: 12px;
@primary_width: 708px;
@secondary_width: 204px;
/* 変数同士の計算 */
@row_width: @primary_width + @gutter_width;
/* 割ったり引いたり */
@half_width: (@row_width / 2) - @gutter_width;
@third_width: (@row_width / 3) - @gutter_width;
@fourth_width: (@row_width / 4) - @gutter_width;
@fifth_width: (@row_width / 5) - @gutter_width;
@sixth_width: (@row_width / 6) - @gutter_width;
@tenth_width: (@row_width / 10) - @gutter_width;
/* 出力フォーマット */
.prepend,.append,.col{margin-right:@gutter_width;}
.primary {width:@primary_width;}
.primary .row {width:@row_width;}
.secondary {width:@secondary_width;}
.secondary .row {width:@secondary_width + @gutter_width;}
.half {width:@half_width;}
.third {width:@third_width;}
.fourth{width:@fourth_width;}
.fifth {width:@fifth_width;}
.sixth {width:@sixth_width;}
.tenth {width:@tenth_width;}
とかすると
.prepend, .append, .col { margin-right: 12px; }
.primary { width: 708px; }
.primary .row { width: 720px; }
.secondary { width: 204px; }
.secondary .row { width: 216px; }
.half { width: 348px; }
.third { width: 228px; }
.fourth { width: 168px; }
.fifth { width: 132px; }
.sixth { width: 108px; }
.tenth { width: 60px; }
こんな感じ。
先に、変数同士で演算した方も、フォーマットする時に演算した方も動いてるみたいですね。
使い道が思いつきませんが
.color{color:#c00 + 111;}
こんな事も出来るようです。
その他
LESSファイルを「@import」のように書いて読み込めるみたいです。
@import "style";
この状態でコンパイルすると、複数ファイルに「いつも使うやつ色々」「フォームとか」「タイポグラフィ関連」「色とか背景とかspriteとか」「言語違い」みたいに分けたファイルを一気に読み込んで、一枚にまとめちゃったりも出来ます。
これは、管理とパフォーマンスの事とか考えたら便利かもしれません。
ここまで試してみての感想
個人的に気になったのは、ネストした時の動作で、
.editable {
h1,h2,h3,h4,h5,h6{color:#000;font-weight:bold;}
}
とか書いたものをコンパイルすると
.editable h1 {
color: #000000;
font-weight: bold;
}
.editable h2 {
color: #000000;
font-weight: bold;
}
.editable h3 {
color: #000000;
font-weight: bold;
}
.editable h4 {
color: #000000;
font-weight: bold;
}
.editable h5 {
color: #000000;
font-weight: bold;
}
.editable h6 {
color: #000000;
font-weight: bold;
}
こんな感じになっちゃうんだけど
.editable h1, .editable h2, .editable h3, .editable h4, .editable h5, .editable h6 {
color: #000000;
font-weight: bold;
}
こんな感じにして欲しいと思いました(まあ、コンパイル後にフォーマットすればイイけど)。
ただ、みんなで作業する場合、こういう(ちょっぴりプログラミング的な)感じがダメな人だとマイナスかもしれんし、どうなのかなーと思います。
個人的にはいつもネストやインデントなしで書いてるから、特に読みやすいとも思わないし......
ただし、「CSS3をガッツリ使うから、先行実装用とか、たくさん書くのめんどいー」という案件では良い気がします。
最初に試してから時間経ってるので、また、試してみよーかなー。
あと、Codaのプラグインもあるみたいです
LESS CSS Plugin for Coda