11.1 Sass (Syntactically Awesome Stylesheets)
CSS預處理器 是一些工具,可以擴展CSS的功能,並且簡化撰寫樣式的過程。它們提供變數、嵌套規則、mixin和函數等功能,使代碼更加模組化、可重用且易於維護。今天我們來看看最流行的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>段落 1</p>
<p>段落 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>段落 1</p>
<p>段落 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>段落 1</p>
<p>段落 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