<h2>Sass十分钟入门</h2>
<h3>变量</h3>
<p>sass中可以定义变量,方便统一修改和维护。</p>
<div class="compare-code">
<pre><code>//sass style
//-----------------------------------
$fontStack: Helvetica, sans-serif;
$primaryColor: #333;
body {
font-family: $fontStack;
color: $primaryColor;
}
</code></pre>
<pre><code>//css style
//-----------------------------------
body {
font-family: Helvetica, sans-serif;
color: #333;
}
</code></pre>
</div>
<h3>嵌套</h3>
<p>sass可以进行选择器的嵌套,表示层级关系,看起来很优雅整齐。</p>
<div class="compare-code">
<pre><code>//sass style
//-----------------------------------
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
</code></pre>
<pre><code>//css style
//-----------------------------------
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
nav a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
</code></pre>
</div>
<h3>导入</h3>
<p>sass中如导入其他sass文件,最后编译为一个css文件,优于纯css的<code>@import</code></p>
<div class="compare-code">
<pre><code>//sass style
//-----------------------------------
// _reset.scss
html,
body,
ul,
ol {
margin: 0;
padding: 0;
}
</code></pre>
<pre><code>//sass style
//-----------------------------------
// base.scss
@import 'reset';
body {
font-size: 100% Helvetica, sans-serif;
background-color: #efefef;
}
</code></pre>
<pre><code>//css style
//-----------------------------------
html, body, ul, ol {
margin: 0;
padding: 0;
}
body {
background-color: #efefef;
font-size: 100% Helvetica, sans-serif;
}
</code></pre>
</div>
<h3>mixin</h3>
<p>sass中可用mixin定义一些代码片段,且可传参数,方便日后根据需求调用。从此处理css3的前缀兼容轻松便捷。</p>
<div class="compare-code">
<pre><code>//sass style
//-----------------------------------
@mixin box-sizing ($sizing) {
-webkit-box-sizing:$sizing;
-moz-box-sizing:$sizing;
box-sizing:$sizing;
}
.box-border{
border:1px solid #ccc;
@include box-sizing(border-box);
}
</code></pre>
<pre><code>//css style
//-----------------------------------
.box-border {
border: 1px solid #ccc;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
</code></pre>
</div>
<h3>扩展/继承</h3>
<p>sass可通过<code>@extend</code>来实现代码组合声明,使代码更加优越简洁。</p>
<div class="compare-code">
<pre><code>//sass style
//-----------------------------------
.message {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
@extend .message;
border-color: green;
}
.error {
@extend .message;
border-color: red;
}
.warning {
@extend .message;
border-color: yellow;
}
</code></pre>
<pre><code>//css style
//-----------------------------------
.message, .success, .error, .warning {
border: 1px solid #cccccc;
padding: 10px;
color: #333;
}
.success {
border-color: green;
}
.error {
border-color: red;
}
.warning {
border-color: yellow;
}
</code></pre>
</div>
<h3>运算</h3>
<p>sass可进行简单的加减乘除运算等</p>
<div class="compare-code">
<pre><code>//sass style
//-----------------------------------
.container { width: 100%; }
article[role="main"] {
float: left;
width: 600px / 960px * 100%;
}
aside[role="complimentary"] {
float: right;
width: 300px / 960px * 100%;
}
</code></pre>
<pre><code>//css style
//-----------------------------------
.container {
width: 100%;
}
article[role="main"] {
float: left;
width: 62.5%;
}
aside[role="complimentary"] {
float: right;
width: 31.25%;
}
</code></pre>
</div>
<h3>颜色</h3>
<p>sass中集成了大量的颜色函数,让变换颜色更加简单。</p>
<div class="compare-code">
<pre><code>//sass style
//-----------------------------------
$linkColor: #08c;
a {
text-decoration:none;
color:$linkColor;
&:hover{
color:darken($linkColor,10%);
}
}
</code></pre>
<pre><code>//css style
//-----------------------------------
a {
text-decoration: none;
color: #0088cc;
}
a:hover {
color: #006699;
}
</code></pre>
</div>
</div>