当前位置: 首页 > 图文教程 > 网页制作 > CSS样式表 > CSS优化:less优化CSS

CSS样式表
提高网页效率的14条注意事项图文
提高网页的效率 Use YSlow to know why your web Slow
学习WEB标准总结的一些CSS/XHTML知识小结
CSS文件可维护、可读性提高指南
Firefox下样式设置宽度奇怪现象
Chrome的hack写法以及CSS的支持程度图示
IE6支持position:fixed完美解决方法
css为图片设置背景图片
ASP、PHP与javascript根据时段自动切换CSS皮肤的代码
css图片切换效果代码[不用js]
css import与link的区别
BS项目中的CSS架构_仅加载自己需要的CSS
div结合css布局bbs首页(div+css布局入门)
css 兼容性问题this.style.cursor=''''hand''''
CSS渐变统计柱形图
跨浏览器的实践:position:fixed 层的固定定位
标准布局常见问题及解决办法
css美化input file按钮的代码方法
重置默认样式 css reset
支持IE6 IE7 Firefox 的纯CSS的下拉菜单

CSS样式表 中的 CSS优化:less优化CSS


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 79 ::
收藏到网摘: n/a

less官方网址:http://lesscss.org

下面就来介绍下吧

less用变量 (variables),引用(mixins),表达式(operations),嵌套规则(nested rules)来扩展css开发

 

变量 (variables)
重复使用的值可以定义成变量的形式,方便更改哈
例子如下:

#header { color: #4D926F;
}
h2 { color: #4D926F;
}
@brand_color: #4D926F;
#header { color: @brand_color;
}
h2 { color: @brand_color;
}

 

 

引用(mixins)
在一个类中可以引用另一个类的名称做为该类的属性。

#header { -moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;
}
#footer { -moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;
}
.rounded_corners { -moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;
}
#header { .rounded_corners;
}
#footer { .rounded_corners;
}

嵌套规则(nested rules)
以前我们开发的css的时候selector的继承都要写的很长 ,用less后。我们可以用更简洁,直观的方式来写css
实例如下:

#header { color: red;
}
#header a { font-weight: bold; text-decoration: none;
}
#header { color: red; a { font-weight: bold; text-decoration: none; }
}

 

表达式(operations)
一些单元之间可能有些值要成比例,比如宽高,颜色值
我们都可以用表达式来计算实现

#header { color: #333; border-left: 1px; border-right: 2px;
}
#footer { color: #222;
}
@the-border: 1px;
@base-color: #111;
#header { color: @base-color * 3; border-left: @the-border; border-right: @the-border * 2;
}
#footer { color: @base-color + #111;
}