11.1 Sass (Syntactically Awesome Stylesheets)
CSS的预处理器 — 是一些工具,可以扩展CSS的功能并简化样式的编写过程。它们添加了变量、嵌套规则、mixin 和 function等,使代码更加模块化、可重用且易于维护。今天我们来看看最流行的CSS预处理器:Sass, LESS 和 Stylus。
Sass — 是最流行的CSS预处理器之一,提供了许多有用的样式功能。
安装 Sass
Sass 可以通过 npm 安装:
Terminal
npm install -g sass
Sass 的主要功能
1. 变量
Sass中的变量允许存储可以重复使用的值:
HTML
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</body>
</html>
Sass
$primary-color: #333
body
color: $primary-color
2. 嵌套规则
嵌套规则允许用更层次的风格书写CSS:
HTML
<html>
<head>
<style>
nav {
background: #eee;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
color: tomato;
}
</style>
</head>
<body>
<nav>
<ul>
<li>元素 1</li>
<li>元素 2</li>
<li>元素 3</li>
</ul>
</nav>
</body>
</html>
Sass
nav
background: #eee
ul
margin: 0
padding: 0
list-style: none
li
color: tomato
3. Mixin
Mixin允许创建可以包含在其他规则中的样式组:
HTML
<html>
<head>
<style>
.box {
width: 256px;
min-height: 200px;
border-radius: 10px;
border: 2px solid green;
}
</style>
</head>
<body>
<div class="box">内容</div>
</body>
</html>
Sass
@mixin border-radius($radius)
-webkit-border-radius: $radius
-moz-border-radius: $radius
border-radius: $radius
.box
@include border-radius(10px)
border: 2px solid green
width: 256px
min-height: 200px
4. 继承
继承允许一个选择器继承另一个选择器的样式:
HTML
<html>
<head>
<style>
.box {
min-height: 120px;
}
.box:not(:last-child) {
margin-bottom: 5px;
}
.info, .success, .warning {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
background-color: #cfc;
}
.warning {
background-color: #fc9
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box info">信息</div>
<div class="box success">成功</div>
<div class="box warning">警告!</div>
</div>
</body>
</html>
Sass
%message
border: 1px solid #ccc
padding: 10px
color: #333
.box
min-height: 120px
&:not(:last-child)
margin-bottom: 5px
.info
@extend %message
.success
@extend %message
background-color: #cfc
.warning
@extend %message
background-color: #fc9
5. 函数
HTML
<html>
<head>
<style>
body {
font-size: 1.125rem;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad cupiditate esse libero veritatis voluptatem?
Aliquam atque laboriosam porro recusandae totam.
</p>
</body>
</html>
Sass
@function px-to-rem($px, $base-font-size: 16px)
@return ($px / $base-font-size) * 1rem;
body
font-size: px-to-rem(18px);
Sass 编译
Sass 编译成 CSS 的命令:
Terminal
sass style.scss style.css
11.2 LESS
安装 LESS
LESS 可以通过 npm 安装:
Terminal
npm install -g less
LESS 的主要功能
1. 变量:
HTML
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</body>
</html>
LESS
@primary-color: #333;
body {
color: @primary-color;
}
2. 嵌套规则:
HTML
<html>
<head>
<style>
nav {
background: #eee;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
color: tomato;
}
</style>
</head>
<body>
<nav>
<ul>
<li>元素 1</li>
<li>元素 2</li>
<li>元素 3</li>
</ul>
</nav>
</body>
</html>
LESS
nav {
background: #eee;
ul {
margin: 0;
padding: 0;
list-style: none;
li {
color: tomato;
}
}
}
HTML
<html>
<head>
<style>
.box {
width: 256px;
min-height: 200px;
border-radius: 10px;
border: 2px solid green;
}
</style>
</head>
<body>
<div class="box">内容</div>
</body>
</html>
3. Mixin:
LESS
.border-radius(@radius) {
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
border-radius: @radius;
}
.box {
.border-radius(10px);
border: 2px solid green;
width: 256px;
min-height: 200px;
}
4. 继承:
LESS 使用符号 "&" 来引用父级。
HTML
<html>
<head>
<style>
button:hover {
background-color: #555;
}
</style>
</head>
<body>
<button>按钮</button>
</body>
</html>
LESS
.button {
&:hover {
background-color: #555;
}
}
5. 函数:
LESS 支持用于处理颜色、数字和字符串的函数。
HTML
<html>
<head>
<style>
body {
font-size: 1.125rem;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad cupiditate esse libero veritatis voluptatem?
Aliquam atque laboriosam porro recusandae totam.
</p>
</body>
</html>
LESS
@base: 16px;
.font-size(@size) {
font-size: (@size / @base) * 1rem;
}
body {
.font-size(18px);
}
LESS 编译
LESS 编译成 CSS 的命令:
Terminal
lessc style.less style.css
11.3 Stylus
Stylus — 是一个灵活而强大的CSS预处理器,提供简洁的语法和许多实用功能。
安装 Stylus
Stylus 可以通过 npm 安装:
Terminal
npm install -g stylus
Stylus 的主要功能
1. 变量:
HTML
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</body>
</html>
Stylus
primary-color = #333
body
color primary-color
2. 嵌套规则:
HTML
<html>
<head>
<style>
nav {
background: #eee;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
color: tomato;
}
</style>
</head>
<body>
<nav>
<ul>
<li>元素 1</li>
<li>元素 2</li>
<li>元素 3</li>
</ul>
</nav>
</body>
</html>
Stylus
nav
background #eee
ul
margin 0
padding 0
list-style none
li
color tomato
3. Mixin:
HTML
<html>
<head>
<style>
.box {
width: 256px;
min-height: 200px;
border-radius: 10px;
border: 2px solid green;
}
</style>
</head>
<body>
<div class="box">内容</div>
</body>
</html>
Stylus
border-radius(radius)
-webkit-border-radius radius
-moz-border-radius radius
border-radius radius
.box
border-radius(10px)
border 2px solid green
width 256px
min-height 200px
4. 继承:
Stylus 使用 @extend
来进行继承。
HTML
<html>
<head>
<style>
.box {
min-height: 120px;
}
.box:not(:last-child) {
margin-bottom: 5px;
}
.info, .success, .warning {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
background-color: #cfc;
}
.warning {
background-color: #fc9
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box info">信息</div>
<div class="box success">成功</div>
<div class="box warning">警告!</div>
</div>
</body>
</html>
Stylus
.message
border 1px solid #ccc
padding 10px
color #333
.info
@extend .message
.success
@extend .message
background-color #cfc
.warning
@extend .message
background-color #fc9
5. 函数:
Stylus 允许创建函数来重复使用。
HTML
<html>
<head>
<style>
body {
font-size: 1.125rem;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad cupiditate esse libero veritatis voluptatem?
Aliquam atque laboriosam porro recusandae totam.
</p>
</body>
</html>
Stylus
px-to-rem(px, base-font-size = 16px)
px / base-font-size * 1rem
body
font-size px-to-rem(18px)
Stylus 编译
Stylus 编译成 CSS 的命令:
Terminal
stylus style.styl
GO TO FULL VERSION