我想弄清楚Joni Korpi的Frameless CSS无框网格(http://framelessgrid.com/)并且我很难阅读他所拥有的.less文件。我基本了解LESS使用变量,所以我知道column = 48和gutter = 24就是这样。
1cols = 1 *(48 + 24) - 24)/ 12?
我不明白的是@1col: @1cols;
和.width (@cols:1) {
width: (@cols * (@column + @gutter) - @gutter) / @em;
}
有人可以帮忙吗?
https://github.com/jonikorpi/Frameless/blob/master/frameless.less
@font-size: 16; // Your base font-size in pixels
@em: @font-size*1em; // Shorthand for outputting ems, e.g. "12/@em"
@column: 48; // The column-width of your grid in pixels
@gutter: 24; // The gutter-width of your grid in pixels
//
// Column-widths in variables, in ems
//
@1cols: ( 1 * (@column + @gutter) - @gutter) / @em; @1col: @1cols;
@2cols: ( 2 * (@column + @gutter) - @gutter) / @em;
@3cols: ( 3 * (@column + @gutter) - @gutter) / @em;
@4cols: ( 4 * (@column + @gutter) - @gutter) / @em;
@5cols: ( 5 * (@column + @gutter) - @gutter) / @em;
@6cols: ( 6 * (@column + @gutter) - @gutter) / @em;
@7cols: ( 7 * (@column + @gutter) - @gutter) / @em;
@8cols: ( 8 * (@column + @gutter) - @gutter) / @em;
@9cols: ( 9 * (@column + @gutter) - @gutter) / @em;
@10cols: (10 * (@column + @gutter) - @gutter) / @em;
@11cols: (11 * (@column + @gutter) - @gutter) / @em;
@12cols: (12 * (@column + @gutter) - @gutter) / @em;
@13cols: (13 * (@column + @gutter) - @gutter) / @em;
@14cols: (14 * (@column + @gutter) - @gutter) / @em;
@15cols: (15 * (@column + @gutter) - @gutter) / @em;
@16cols: (16 * (@column + @gutter) - @gutter) / @em;
//
// Column-widths in a function, in ems
//
.width (@cols:1) {
width: (@cols * (@column + @gutter) - @gutter) / @em;
}
答案 0 :(得分:3)
@1cols
等只是变量名。允许less
中的变量名以数字开头。
@1col: @1cols;
这只是说变量@1col
等于之前设置的变量@1cols
。据推测,“1col”因为1是单数,但其他是复数,所以它只是让你选择使用@1col
或@1cols
它们都是相同的值。
@1cols: ( 1 * (@column + @gutter) - @gutter) / @em;
那只是数学。如果你想要一个3列宽的部分,那就是(列宽+沟宽)减去一个沟槽的3倍。
.width (@cols:1) {
width: (@cols * (@column + @gutter) - @gutter) / @em;
}
这是一个mixin函数,它使用默认参数为1的可变数量的列。您可以像这样使用它:
.my-class{
.width(3);
}
/* these two are identical */
.my-class{
width: @3cols;
}
第一种方法的好处是您可以将3
替换为变量,以便在其他地方使用它。
答案 1 :(得分:1)
@
是一个变量标识符...类似于php中的$
。
所以他正在做的是定义一个mixin,它在某些方面就像一个函数,如果没有提供,它会使参数@cols
的默认值为1
。然后,这个mixin将width
css属性设置为表达式的值:
(@cols * (@column + @gutter) - @gutter) / @em;
我认为您的@em
值将是1em的值(以像素为单位)。因此,如果您的基本字体大小为12,那么@em
= 12。
至于@1col: @1cols;
只是为了方便你可以使用@1col
(单数)或@1cols
(复数),它们的意思相同。
答案 2 :(得分:1)
其他答案很好地解释了LESS文件的作用,所以我将谈谈他对@em
的使用。
如果你这样做
.some_class {
just_an_em: @em
}
在.less文件中,它会出现
.come_class {
just_an_em: 16em
}
编译后在.css中。这似乎是因为LESS在分割时保留了单位,因此'16 / @ em'给出'1em',正如预期的那样。