CSS 预处理器框架

CSS 预处理器框架

参考博客:为您详细比较三个 CSS 预处理器(框架):Sass、LESS 和 Stylus

可以按照需求来使用别人的代码

1.sass (compass)

2.less (lesshat/EST)

3.提供现成的 mixin

4.类似 JS 类库 ,封装常用功能  

css 预处理器常见问题(详细讲解见上篇博客

1.常见的 css 预处理器

1.Less(Node.js)  2.Sass (Ruby 但有 Node 版本)

2.预处理器作用

1.帮助更好地组织 CSS 代码

2.提高代码复用率

3.提升可维护性

3.预处理器能力

1.嵌套  反映层级和约束

2.变量和计算  减少重复代码

3.Extend 和 Mixin 代码片段

4.循环  适用于复杂有规律的样式

5.import CSS 文件模块化

4.预处理器的优缺点

优点:提高代码复用率和可维护性

缺点:需要引入编译过程 有学习成本

演示:

less

 

.box{
    .inline-block();
    .opacity(60);
    height: 100px;
    background: green;
    margin:10px;
}
.left{
    float:left;
    .clearfix();
}


.row{
    .make-row();
    .col{
        .make-column(1/4);
        background:red;
        height: 100px;
    }
}
.my-triangle{
    margin:100px;
    // width:100px;
    // height:200px;
    // border: 1px solid red;
}
.my-triangle::after{
    content: ' ';
    .triangle(top left, 100px, red, side);
}

 

  html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="7-est.css">
    <title>Document</title>
</head>
<body>
    <div class="box">.box box1</div>
    <div class="box">.box box2</div>
    <div class="left">.left</div>
    <div class="row">
        <div class="col">col1</div>
        <div class="col">col2</div>
        <div class="col">col3</div>
        <div class="col">col4</div>
    </div>
    <div class="my-triangle"></div>
</body>
</html>

  css

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td {
  padding: 0;
  margin: 0;
  border: 0;
  outline: 0;
  font-weight: inherit;
  font-style: inherit;
  font-family: inherit;
  font-size: 100%;
  vertical-align: baseline;
}
body {
  line-height: 1;
}
ol,
ul {
  list-style: none;
}
table {
  border-collapse: separate;
  border-spacing: 0;
  vertical-align: middle;
}
caption,
th,
td {
  text-align: left;
  font-weight: normal;
  vertical-align: middle;
}
a img {
  border: none;
}
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section,
summary,
main {
  display: block;
  padding: 0;
  margin: 0;
  border: 0;
  outline: 0;
  font-weight: inherit;
  font-style: inherit;
  font-family: inherit;
  font-size: 100%;
  vertical-align: baseline;
}
audio,
canvas,
video {
  display: inline-block;
  *display: inline;
  *zoom: 1;
}
audio:not([controls]),
[hidden] {
  display: none;
}
.box {
  display: inline-block;
  *display: inline;
  *zoom: 1;
  opacity: 0.6;
  filter: alpha(opacity=60);
  height: 100px;
  background: green;
  margin: 10px;
}
.left {
  float: left;
  *zoom: 1;
}
.left:before,
.left:after {
  display: table;
  content: "";
}
.left:after {
  clear: both;
}
.row {
  *zoom: 1;
}
.row:before,
.row:after {
  display: table;
  content: "";
}
.row:after {
  clear: both;
}
.row .col {
  display: block;
  float: left;
  width: 22.75%;
  margin-left: 3%;
  background: red;
  height: 100px;
}
.row .col:first-child {
  margin-left: 0%;
}
.my-triangle {
  margin: 100px;
}
.my-triangle::after {
  content: ' ';
  position: absolute;
  width: 0;
  height: 0;
  border: 50px solid red;
  border-bottom-color: transparent;
  border-right-color: transparent;
  margin-top: -50px;
  margin-left: -50px;
}
不同的 CSS 预处理器在语法、功能特性、社区支持等方面存在一定区别。常见的 CSS 预处理器有 Sass、Less 和 Stylus。 ### 语法差异 - **Sass**:有两种语法格式,一种是 Sass 格式(以 .sass 为扩展名),使用缩进来表示嵌套关系,不使用花括号和分号;另一种是 SCSS 格式(以 .scss 为扩展名),使用与 CSS 类似的语法,使用花括号和分号。例如,Sass 格式: ```sass body font-size: 16px color: #333 ``` SCSS 格式: ```scss body { font-size: 16px; color: #333; } ``` - **Less**:语法与 CSS 非常相似,使用花括号和分号,易于上手。例如: ```less body { font-size: 16px; color: #333; } ``` - **Stylus**:语法灵活,可以使用缩进来表示嵌套关系,也可以使用花括号和分号,甚至可以省略引号。例如: ```stylus body font-size 16px color #333 ``` ### 功能特性差异 - **变量**:三者都支持变量,但 Sass 和 Less 的变量使用 `$` 符号开头,而 Stylus 可以使用 `$` 符号,也可以不使用。例如,Sass 和 Less: ```scss $primary-color: #007bff; body { color: $primary-color; } ``` Stylus: ```stylus primary-color = #007bff body color primary-color ``` - **混合(Mixins)**:Sass 和 Less 都有专门的 `@mixin` 和 `@include` 来定义和使用混合,而 Stylus 可以直接定义函数来实现类似功能。例如,Sass: ```scss @mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; border-radius: $radius; } .button { @include border-radius(5px); } ``` Stylus: ```stylus border-radius(radius) -webkit-border-radius radius -moz-border-radius radius border-radius radius .button border-radius(5px) ``` - **函数**:Sass 内置了丰富的函数,如颜色处理函数、数学函数等;Less 也有一些内置函数,但相对 Sass 较少;Stylus 的函数功能也很强大,并且可以自定义函数。 ### 社区支持和生态系统 - **Sass**:拥有庞大的社区和丰富的生态系统,有很多成熟的框架和工具基于 Sass 开发,如 Compass 等。 - **Less**:在前端社区也有广泛的应用,与 Bootstrap 框架结合紧密,许多 Bootstrap 主题使用 Less 编写。 - **Stylus**:社区相对较小,但功能强大,适合对灵活性要求较高的项目。 ### 编译性能 不同的 CSS 预处理器编译速度可能会有所不同,一般来说,Less 的编译速度相对较快,而 Sass 在大型项目中的编译性能也不错。 综上所述,不同的 CSS 预处理器各有优缺点,开发者可以根据项目需求、团队技术栈和个人偏好来选择合适的预处理器
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值