Less学习总结

say hello & intro

  • Less is More,Than CSS-少即是多,比CSS

less介绍-say hello

Less是什么?

Less 类似于Jquery

  • LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS的赋予了动态语言的特性,如变量、继承、运算、函数等,更方便CSS的编写和维护。
  • LESSCSS可以在多种语言、环境中使用,包括浏览器端、桌面客户端、服务端。

Koala的基本使用

koala编译

Node.js库编译

  • npm install -g less
  • lessc test.less > test.css(注意less文件与css文件路径)

浏览器端编译

Less语法详解

Less注释

less代码

/*我是被编译的*/

//不会被编译

css代码

/*我是被编译的*/

变量

  • less中想要声明变量的话 一定要用@开头 例如: @变量名:值;

less代码

//变量

@test_width:300px;

.body{
  width:@test_width;
  height:@test_width;
  background-color: yellow;

  .border;

  left:100px;
}

css代码

.body {
  width: 300px;
  height: 300px;
  background-color: yellow;
  border: 5px solid pink;
  left: 100px;
}

混合

混合(mixin)变量

例如:

.border{border:solid 10px red;}

带参数的混合

.border-radius (@radius) {css代码}

可设定默认值

border-radius(@radius:5px){css代码}

简单例子

less 代码

.body{
  width:@test_width;
  height:@test_width;
  background-color: yellow;

  .border;

  left:100px;
}

//混合

.border{
  border:5px solid pink;
}

css代码

.body {
  width: 300px;
  height: 300px;
  background-color: yellow;
  border: 5px solid pink;
  left: 100px;
}
.border {
  border: 5px solid pink;
}

复杂例子

less代码

//混合 - 可带参数的

.border_02(@border_width){
  border: solid yellow @border_width;
}

.test_hunhe{
  .border_02(30px);
}

//混合 - 默认带值
.border_03(@border_width:10px){
  border:solid green @border_width;
}

.test_hunhe_03{
  .border_03();
}

.test_hunhe_03{
  .border_03(50px);
}

//混合的例子

.border_radius(@radius:5px){
  -webkit-border-radius: @radius;
  -moz-border-radius: @radius;
  border-radius: @radius;
}

.radius_test{
  width:100px;
  height:40px;
  background-color: green;

  .border_radius();
}

.sanjiao{
  .triangle(top,100px);
}

css代码

.test_hunhe {
  border: solid yellow 30px;
}
.test_hunhe_03 {
  border: solid green 10px;
}
.test_hunhe_03 {
  border: solid green 50px;
}
.radius_test {
  width: 100px;
  height: 40px;
  background-color: green;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.sanjiao {
  width: 0;
  height: 0;
  overflow: hidden;
  border-width: 10px;
  border-color: transparent transparent red transparent;
  border-style: dashed dashed solid dashed;
}

匹配模式

  • 相当于JS中的if(但不完全是)
  • 满足条件后才能匹配

less代码

//匹配模式
.triangle(top,@w:5px,@c:#ccc){
    border-width: @w;
    border-color: transparent transparent @c transparent;
    border-style: dashed dashed solid dashed;
}

.triangle(bottom,@w:5px,@c:#ccc){
  border-width: @w;
  border-color: @c transparent transparent transparent;
  border-style: solid dashed dashed dashed;
}

.triangle(left,@w:5px,@c:#ccc){
  border-width: @w;
  border-color: transparent @c transparent transparent;
  border-style: dashed solid dashed dashed;
}

.triangle(right,@w:5px,@c:#ccc){
  border-width:@w;
  border-color:transparent transparent transparent @c;
  border-style: dashed dashed dashed solid;
}
.sanjiao{
  .triangle(top,100px);
}

//匹配模式 - 定位
.pos(r){
  position:relative;
}

.pos(a){
  position:absolute;
}

.pos(f){
  position:fixed;
}

.pipe{
  width:200px;
  height:200px;
  background-color: green;
  .pos(r);
}

css代码

.sanjiao {
  border-width: 100px;
  border-color: transparent transparent #ccc transparent;
  border-style: dashed dashed solid dashed;
}
.pipe {
  width: 200px;
  height: 200px;
  background-color: green;
  position: relative;
}

运算

  • 任何数字,颜色或者变量都可以参与运算,运算应该可以包裹在括号中。例如==:+ - * /==

less代码

//运算

@test_01:300px;

.box_02{
  width:@test_01 + 20;
}

css代码

.box_02 {
  width: 320px;
}

嵌套规则

  • Less中最有意思的小东西
  • 两种用法

(1)&对尾类使用
hover或者focus

(2)对连接的使用&-item


less代码

//嵌套规则

/*
.list{}
.list li{}
.list a{}
.list span{}
*/

.list{
  width:600px;
  margin:30px auto;
  padding:0;
  list-style:none;

  li{
    height: 30px;
    line-height: 30px;
    background-color: pink;
    margin-bottom: 5px;
    padding:0 10px;
  }
  a{
    float:left;
    //&代表他的上一层选择器
    &:hover{
      color:red;
    }
  }
  span{
    float:right;
  }
}

css代码

/*
.list{}
.list li{}
.list a{}
.list span{}
*/
.list {
  width: 600px;
  margin: 30px auto;
  padding: 0;
  list-style: none;
}
.list li {
  height: 30px;
  line-height: 30px;
  background-color: pink;
  margin-bottom: 5px;
  padding: 0 10px;
}
.list a {
  float: left;
}
.list a:hover {
  color: red;
}
.list span {
  float: right;
}

@arguments变量

  • @arguments包含了所有传递进来的参数
  • 如果你不想单独处理每一个参数的话就可以像这样写:

less代码

.border_arg(@w:30px,@c:red,@xx:solid){
  border:@arguments;  //相当于传入所有变量:border:@w @c @xx;
}

.test_arguments{
  .border_arg(40px);
}

css代码

.test_arguments {
  border: 40px red solid;
}

避免编译,!important以及总结

避免编译

  • 有时候我们需要输出一些不正确的CSS语法或者使用一些Less不认识的专有语法。
  • 要输出这样的值我们可以在字符串
    前面加上一个~例如:width:~'calc(100%-35)'

less代码

//避免编译

.test_03{
  width:~'calc(300px - 30px)';
}

css代码

.test_03 {
  width: calc(300px - 30px);
}

!important关键字

  • 会为所有混合所带来的样式,添加上!important

less代码

//!important
.border_radius(@radius:5px){
  -webkit-border-radius: @radius;
  -moz-border-radius: @radius;
  border-radius: @radius;
}

.test_important{
  .border_radius()!important;
}

css代码

.test_important {
  -webkit-border-radius: 5px !important;
  -moz-border-radius: 5px !important;
  border-radius: 5px !important;
}

简单实战项目

Snip20170721_54.png

项目代码

更多

Less中文网站

http://lesscss.net/

如果看不懂英文的,可以访问less中文旧版网站

http://old.lesscss.net/article/document.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • Less是什么 Less类似于jQuery。 ——是一种css动态语言,属于css预处理语言的一种。它使用类似cs...
    罂粟1995阅读 536评论 1 0
  • [目录] Less为什么会出现? 学习Less的网站 Less的安装环境离线的安装方式在线的安装方式 less转化...
    顽皮的雪狐七七阅读 15,129评论 5 42
  • 大家好,我是IT修真院郑州分院第05期学员,一枚正直纯洁善良的web程序员。今天给大家分享一下,修真院官网css任...
    渣渣啊123阅读 2,387评论 0 4
  • 来德国之前就已得知新天鹅堡在德国旅游中拥有超高的人气,它那童话般的景致与气质让无数游人为之倾倒,于是心念念了好几个...
    一叶飘零6056阅读 832评论 0 4
  • 面对夜晚一个人的教室,我想回家了。我想写作了。这一刻,我有些疲累了。 不累,只是眼睛累; 有累,可以是错觉。 闭上...
    乡知阅读 289评论 0 0