前端之Sass/Scss实战笔记
<div class="article_meta" style="color: rgb(153, 153, 153); padding-bottom: 5px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(211, 211, 211);"><div class="source"><span style="float: left;">原文</span> <a class="cut cut70" href="http://segmentfault.com/a/1190000003742313?utm_source=tuicool&utm_medium=referral" style="color: rgb(51, 51, 51); text-decoration: none; transition: 0.25s; outline: none 0px; overflow: hidden; white-space: nowrap; word-break: keep-all; text-overflow: ellipsis; max-width: 69%; display: inline-block;">http://segmentfault.com/a/1190000003742313</a></div><div><span style="margin-right: 5px;">主题</span> <a href="http://www.tuicool.com/topics/11060038" target="_blank" style="color: rgb(51, 51, 51); text-decoration: none; transition: 0.25s; outline: none 0px;"><span class="new-label" style="display: inline-block; padding: 2px 4px; font-size: 0.9em; line-height: 16px; vertical-align: baseline; white-space: nowrap; color: rgb(120, 120, 120); margin-right: 5px; background-color: rgb(242, 242, 242);">Sass</span></a></div></div><div class="article_body" id="nei" style="padding: 20px 5px 25px; overflow-x: hidden; word-break: break-all; min-height: 340px; font-size: 16px; line-height: 1.7em;"><div style="line-height: 1.7em;"><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">Introduction</h2><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">Sass 有两种语法规则(syntaxes),目前新的语法规则(从 Sass 3开始)被称为 “SCSS”( 时髦的css(Sassy CSS)),它是css3语法的的拓展级,就是说每一个语法正确的CSS3文件也是合法的SCSS文件,SCSS文件使用.scss作为拓展名。第二种语法别成为缩进语法(或者 Sass),它受到了Haml的简洁精炼的启发,它是为了人们可以用和css相近的但是更精简的方式来书写css而诞生的。它没有括号,分号,它使用 行缩进 的方式来指定css 块,虽然sass不是最原始的语法,但是缩进语法将继续被支持,在缩进语法的文件以 .sass 为拓展名。</p><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">注释</h2><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">有三种形式:</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(1)//comment:该注释只是在.scss源文件中有,编译后的css文件中没有。</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(2)/ <em>!</em> /:重要注释,任何style的css文件中都会有,一般放置css文件版权说明等信息。</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(3)/ <em></em>/:该注释在compressed的style的css中没有,其他style的css文件都会含有。</p><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">Quick Start</h2><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">Installation</h3><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">Build</h3><h4 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 27.2px; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; text-indent: 1em; background-color: rgb(254, 254, 254);">1.切换到.scss文件所在目录</h4><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">命令行下切换到代码文件夹目录(如Z:\),假设有文件test.scss文件,里面内容如下:(SASS完全支持css语法)</p><pre class="css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="tag" style="color: rgb(0, 0, 128);">h1</span><span class="rules">{<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">font-size</span>:<span class="value"><span class="number" style="color: rgb(0, 153, 153);">17</span>px;</span></span>
<span class="rule">}</span></span>
<span class="tag" style="color: rgb(0, 0, 128);">h2</span><span class="rules">{
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">font-size</span>:<span class="value"><span class="number" style="color: rgb(0, 153, 153);">18</span>px;</span></span>
<span class="rule">}</span></span></code></pre><h4 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 27.2px; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; text-indent: 1em; background-color: rgb(254, 254, 254);">2.编译scss文件为css文件</h4><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">运行命令:sass --style compressed test.scss test.css,即可生成压缩版的css文件,并且命名为test.css。几点说明:</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(1)--style 后面可以有四个参数可选,分别为expanded、nested、compact、compressed,分别选用不同参数的效果可以自己尝试体验。</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(2)test.scss和test.css文件目录可以自定义,例如把Z盘sass目录下的test.scss文件编译为压缩版的文件,并放置在Z盘css目录下,那么命令即:sass --style compressed z:\sass\test.scss z:\css\test.css</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(3)开发过程中,只需要修改scss文件,然后编译;前端页面只需要引用相应的css文件即可。</p><h4 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 27.2px; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; text-indent: 1em; background-color: rgb(254, 254, 254);">3.侦听文件和文件夹</h4><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">如果希望某一个scss文件或者相应的文件夹下面文件修改后,自动进行编译,那么可以使用侦听命令。</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(1)侦听文件:</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">sass --watch --style compressed test.scss:test.css</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">当test.scss文件有修改后,会自动编译为test.css,并且是compressed的。</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(2)侦听文件夹:</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">sass --watch --style compressed sass:css</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">当sass文件夹下.scss文件有修改的时候,会自动编译为与sass中文件同名的css文件。</p><h4 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 27.2px; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; text-indent: 1em; background-color: rgb(254, 254, 254);">备注:</h4><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(1)注意源文件和目标文件之间是 <strong style="text-indent: 0px;">冒号</strong> ,与编译命令中为空格不同。</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">(2)生成的map文件可以查找source map文件的作用。</p><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">Webpack</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">Webpack中也内置了sass-loader,通过简单的配置既可以使用。不过需要注意的是,Webpack的sass-loader还是依赖于node-sass以及sass(gem),所以如果安装sass-loader报错可以先尝试安装sass。</p><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">变量与选择器</h2><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">变量</h2><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">定义</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">变量的定义一般以$开头,某个变量的作用域仅限于他们定义的层级以及子层级。如果变量是定义在所有嵌套选择器之外的,那么他们可以在各处被调用。</p><pre class="css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">$<span class="tag" style="color: rgb(0, 0, 128);">color1</span>:<span class="id" style="color: rgb(153, 0, 0); font-weight: bold;">#aeaeae</span>;
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">.div1</span><span class="rules">{
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">background-color</span>:<span class="value">$color1;</span></span>
<span class="rule">}</span></span></code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">编译后:</p><pre class="prettyprint css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">.div1</span> <span class="rules">{
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">background-color</span>:<span class="value"> <span class="hexcolor" style="color: rgb(0, 153, 153);">#aeaeae</span>;</span></span>
<span class="rule">}</span></span>
<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">/*# sourceMappingURL=test.css.map */</span></code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">如果希望某个在子选择器中定义的变量能够成为全局变量,可以使用!global关键字:</p><pre class="prettyprint css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="id" style="color: rgb(153, 0, 0); font-weight: bold;">#main</span> <span class="rules">{
<span class="rule">$<span class="attribute" style="color: rgb(0, 128, 128);">width</span>:<span class="value"> <span class="number" style="color: rgb(0, 153, 153);">5</span>em !global;</span></span>
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">width</span>:<span class="value"> $width;</span></span>
<span class="rule">}</span></span>
<span class="id" style="color: rgb(153, 0, 0); font-weight: bold;">#sidebar</span> <span class="rules">{
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">width</span>:<span class="value"> $width;</span></span>
<span class="rule">}</span></span></code></pre><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">嵌套引用</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">嵌套引用在其他编程语言中即是字符串插值,需要用#{}进行包裹:</p><pre class="ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="variable" style="color: rgb(0, 128, 128);">$left</span><span class="symbol" style="color: rgb(153, 0, 115);">:left</span>;
.div1{
border-<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$left}-width:5px;</span>
}</code></pre><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">变量计算</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">Sass中也是支持对于变量进行简单的计算:</p><pre class="css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">$<span class="tag" style="color: rgb(0, 0, 128);">left</span><span class="pseudo">:20px</span>;
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">.div1</span><span class="rules">{
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">margin-left</span>:<span class="value">$left+<span class="number" style="color: rgb(0, 153, 153);">12</span>px;</span></span>
<span class="rule">}</span></span></code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">变量可以支持计算的类型,还是比较多的:</p><pre class="prettyprint ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">p {
<span class="symbol" style="color: rgb(153, 0, 115);">font:</span> <span class="number" style="color: rgb(0, 153, 153);">10</span>px/<span class="number" style="color: rgb(0, 153, 153);">8</span>px; <span class="regexp" style="color: rgb(0, 153, 38);">//</span> <span class="constant">Plain</span> <span class="constant">CSS</span>, no division
<span class="variable" style="color: rgb(0, 128, 128);">$width</span><span class="symbol" style="color: rgb(153, 0, 115);">:</span> <span class="number" style="color: rgb(0, 153, 153);">1000</span>px;
<span class="symbol" style="color: rgb(153, 0, 115);">width:</span> <span class="variable" style="color: rgb(0, 128, 128);">$width</span>/<span class="number" style="color: rgb(0, 153, 153);">2</span>; <span class="regexp" style="color: rgb(0, 153, 38);">//</span> <span class="constant">Uses</span> a variable, does division
<span class="symbol" style="color: rgb(153, 0, 115);">width:</span> round(<span class="number" style="color: rgb(0, 153, 153);">1.5</span>)/<span class="number" style="color: rgb(0, 153, 153);">2</span>; <span class="regexp" style="color: rgb(0, 153, 38);">//</span> <span class="constant">Uses</span> a function, does division
<span class="symbol" style="color: rgb(153, 0, 115);">height:</span> (<span class="number" style="color: rgb(0, 153, 153);">500</span>px/<span class="number" style="color: rgb(0, 153, 153);">2</span>); <span class="regexp" style="color: rgb(0, 153, 38);">//</span> <span class="constant">Uses</span> parentheses, does division
margin-<span class="symbol" style="color: rgb(153, 0, 115);">left:</span> <span class="number" style="color: rgb(0, 153, 153);">5</span>px + <span class="number" style="color: rgb(0, 153, 153);">8</span>px/<span class="number" style="color: rgb(0, 153, 153);">2</span>px; <span class="regexp" style="color: rgb(0, 153, 38);">//</span> <span class="constant">Uses</span> +, does division
<span class="symbol" style="color: rgb(153, 0, 115);">font:</span> (italic bold <span class="number" style="color: rgb(0, 153, 153);">10</span>px/<span class="number" style="color: rgb(0, 153, 153);">8</span>px); <span class="regexp" style="color: rgb(0, 153, 38);">//</span> <span class="constant">In</span> a list, parentheses don<span class="string" style="color: rgb(221, 17, 68);">'t count
}</span></code></pre><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">选择器</h2><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">嵌套</h3><pre class="undefined" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">.div1{
.span1{
height: 12px;
}
.div2{
width: 16px;
}
}</code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">属性也可以嵌套,比如border-color属性,可以写成:</p><pre class="css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="tag" style="color: rgb(0, 0, 128);">p</span> <span class="rules">{
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">border</span>:<span class="value"> {
color: red;</span></span>
<span class="rule">}</span></span>
}</code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">注意,border后面必须加上冒号。</p><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">父元素引用</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">在嵌套的子层级中,允许使用&引用父元素:</p><pre class="ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">.div1{
&<span class="symbol" style="color: rgb(153, 0, 115);">:hover</span>{
<span class="symbol" style="color: rgb(153, 0, 115);">cursor:</span> hand;
}
}</code></pre><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">代码重用</h2><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">继承</h2><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">SASS允许一个选择器,继承另一个选择器。比如,现有class1:</p><pre class="prettyprint ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">.class1{
font-<span class="symbol" style="color: rgb(153, 0, 115);">size:</span><span class="number" style="color: rgb(0, 153, 153);">19</span>px;
}
.class2{
<span class="variable" style="color: rgb(0, 128, 128);">@extend</span> .class1;
<span class="symbol" style="color: rgb(153, 0, 115);">color:</span>black;
}</code></pre><h4 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 27.2px; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; text-indent: 1em; background-color: rgb(254, 254, 254);">注意:如果在class2后面有设置了class1的属性,那么也会影响class2,如下:</h4><pre class="prettyprint ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">.class1{
<span class="indent"></span>font-<span class="symbol" style="color: rgb(153, 0, 115);">size:</span><span class="number" style="color: rgb(0, 153, 153);">19</span>px;
}
.class2{
<span class="indent"></span><span class="variable" style="color: rgb(0, 128, 128);">@extend</span> .class1;
<span class="indent"></span><span class="symbol" style="color: rgb(153, 0, 115);">color:</span>black;
}
.class1{
<span class="indent"></span>font-<span class="symbol" style="color: rgb(153, 0, 115);">weight:</span>bold;
}</code>
</pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">由此可以看出Scss也是递归编译的。</p><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">引用外部css文件(Partials)</h2><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">有时网页的不同部分会分成多个文件来写样式,或者引用通用的一些样式,那么可以使用@import。</p><pre class="prettyprint css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="at_rule">@<span class="keyword" style="font-weight: bold;">import</span> <span class="string" style="color: rgb(221, 17, 68);">"_test1.scss"</span></span>;
<span class="at_rule">@<span class="keyword" style="font-weight: bold;">import</span> <span class="string" style="color: rgb(221, 17, 68);">"_test2.scss"</span></span>;
<span class="at_rule">@<span class="keyword" style="font-weight: bold;">import</span> <span class="string" style="color: rgb(221, 17, 68);">"_test3.scss"</span></span>;</code></pre><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">Mixin&Include</h2><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">Mixin有点像C语言的宏(macro),是可以重用的代码块。</p><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">使用@mixin命令,定义一个代码块。</p><pre class="java" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="annotation">@mixin</span> left {
<span class="keyword" style="font-weight: bold;">float</span>: left;
margin-left: <span class="number" style="color: rgb(0, 153, 153);">10</span>px;
}</code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">使用@include命令,调用这个mixin。</p><pre class="ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 0.75em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">div {
<span class="variable" style="color: rgb(0, 128, 128);">@include</span> left;
}</code></pre><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">参数与缺省值</h3><ul style="margin-bottom: 0.75em; margin-left: 25px; list-style-type: none; line-height: 27.2px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);"><li style="line-height: 1.7em;"><p class="no-text-indent" style="margin-bottom: 0.75em; line-height: 1.7em;">边距设置</p></li></ul><pre class="prettyprint ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="variable" style="color: rgb(0, 128, 128);">@mixin</span> common(<span class="variable" style="color: rgb(0, 128, 128);">$value1</span>,<span class="variable" style="color: rgb(0, 128, 128);">$value2</span>,<span class="variable" style="color: rgb(0, 128, 128);">$defaultValue</span><span class="symbol" style="color: rgb(153, 0, 115);">:</span><span class="number" style="color: rgb(0, 153, 153);">12</span>px){
<span class="indent"></span><span class="symbol" style="color: rgb(153, 0, 115);">display:</span>block;
<span class="indent"></span>margin-<span class="symbol" style="color: rgb(153, 0, 115);">left:</span><span class="variable" style="color: rgb(0, 128, 128);">$value1</span>;
<span class="indent"></span>margin-<span class="symbol" style="color: rgb(153, 0, 115);">right:</span><span class="variable" style="color: rgb(0, 128, 128);">$value2</span>;
<span class="indent"></span><span class="symbol" style="color: rgb(153, 0, 115);">padding:</span><span class="variable" style="color: rgb(0, 128, 128);">$defaultValue</span>;
}
.class1{
<span class="indent"></span>font-<span class="symbol" style="color: rgb(153, 0, 115);">size:</span><span class="number" style="color: rgb(0, 153, 153);">16</span>px;
<span class="indent"></span><span class="variable" style="color: rgb(0, 128, 128);">@include</span> common(<span class="number" style="color: rgb(0, 153, 153);">12</span>px,<span class="number" style="color: rgb(0, 153, 153);">13</span>px,<span class="number" style="color: rgb(0, 153, 153);">15</span>px);
}
.class2{
<span class="indent"></span>font-<span class="symbol" style="color: rgb(153, 0, 115);">size:</span><span class="number" style="color: rgb(0, 153, 153);">16</span>px;
<span class="indent"></span><span class="variable" style="color: rgb(0, 128, 128);">@include</span> common(<span class="number" style="color: rgb(0, 153, 153);">12</span>px,<span class="number" style="color: rgb(0, 153, 153);">13</span>px);
}</code>
</pre><ul style="margin-bottom: 0.75em; margin-left: 25px; list-style-type: none; line-height: 27.2px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);"><li style="line-height: 1.7em;"><p class="no-text-indent" style="margin-bottom: 0.75em; line-height: 1.7em;">浏览器前缀设置设置</p></li></ul><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">下面是一个mixin的实例,用来生成浏览器前缀。</p><pre class="prettyprint ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="variable" style="color: rgb(0, 128, 128);">@mixin</span> rounded(<span class="variable" style="color: rgb(0, 128, 128);">$vert</span>, <span class="variable" style="color: rgb(0, 128, 128);">$horz</span>, <span class="variable" style="color: rgb(0, 128, 128);">$radius</span><span class="symbol" style="color: rgb(153, 0, 115);">:</span> <span class="number" style="color: rgb(0, 153, 153);">10</span>px) {
border-<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$vert}-#{$horz}-radius: $radius;</span>
-moz-border-radius-<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$vert}#{$horz}: $radius;</span>
-webkit-border-<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$vert}-#{$horz}-radius: $radius;</span>
}</code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">使用的时候,可以像下面这样调用:</p><pre class="prettyprint ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#navbar li { <span class="yardoctag">@include</span> rounded(top, left); }</span>
<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#footer { <span class="yardoctag">@include</span> rounded(top, left, 5px); }</span></code></pre><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">编程式方法</h2><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">流程控制</h2><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">条件语句</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">@if可以用来判断:</p><pre class="prettyprint bash" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">p {
@<span class="keyword" style="font-weight: bold;">if</span> 1 + 1 == 2 { border: 1px solid; }
@<span class="keyword" style="font-weight: bold;">if</span> 5 < 3 { border: 2px dotted; }
}</code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">配套的还有@else命令:</p><pre class="prettyprint bash" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">@<span class="keyword" style="font-weight: bold;">if</span> lightness(<span class="variable" style="color: rgb(0, 128, 128);">$color</span>) > 30% {
background-color: <span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#000;</span>
} @<span class="keyword" style="font-weight: bold;">else</span> {
background-color: <span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#fff;</span>
}</code></pre><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">循环语句</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">SASS支持for循环:</p><pre class="prettyprint bash" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">@<span class="keyword" style="font-weight: bold;">for</span> <span class="variable" style="color: rgb(0, 128, 128);">$i</span> from 1 to 10 {
.border-<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$i} {</span>
border: <span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$i}px solid blue;</span>
}
}</code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">也支持while循环:</p><pre class="prettyprint bash" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="variable" style="color: rgb(0, 128, 128);">$i</span>: 6;
@<span class="keyword" style="font-weight: bold;">while</span> <span class="variable" style="color: rgb(0, 128, 128);">$i</span> > 0 {
.item-<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$i} { width: 2em * $i; }</span>
<span class="variable" style="color: rgb(0, 128, 128);">$i</span>: <span class="variable" style="color: rgb(0, 128, 128);">$i</span> - 2;
}</code></pre><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">each命令,作用与for类似:</p><pre class="prettyprint ruby" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="variable" style="color: rgb(0, 128, 128);">@each</span> <span class="variable" style="color: rgb(0, 128, 128);">$member</span> <span class="keyword" style="font-weight: bold;">in</span> a, b, c, d {
.<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#{$member} {</span>
background-<span class="symbol" style="color: rgb(153, 0, 115);">image:</span> url(<span class="string" style="color: rgb(221, 17, 68);">"/image/<span class="subst" style="color: rgb(51, 51, 51);">#{<span class="variable" style="color: rgb(0, 128, 128);">$member</span>}</span>.jpg"</span>);
}
}</code></pre><h2 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">函数</h2><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">Sass允许用户自定义函数,原型如下所示:</p><pre class="prettyprint css" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;"><span class="at_rule">@function <span class="function">double($n)</span> </span>{
<span class="at_rule">@return $n * <span class="number" style="color: rgb(0, 153, 153);">2</span></span>;
}
<span class="id" style="color: rgb(153, 0, 0); font-weight: bold;">#sidebar</span> <span class="rules">{
<span class="rule"><span class="attribute" style="color: rgb(0, 128, 128);">width</span>:<span class="value"> <span class="function">double(<span class="number" style="color: rgb(0, 153, 153);">5</span>px)</span>;</span></span>
<span class="rule">}</span></span></code></pre><h3 style="margin-bottom: 0.5em; font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; line-height: 1.6em; color: rgb(51, 51, 51); text-rendering: optimizeLegibility; font-size: 18px; text-indent: 1em; background-color: rgb(254, 254, 254);">颜色函数</h3><p style="margin-bottom: 0.75em; line-height: 27.2px; text-indent: 1em; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, STXihei, "Microsoft YaHei", 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);">SASS提供了一些内置的颜色函数,以便生成系列颜色。</p><pre class="prettyprint bash" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-bottom: 1.5em; font-size: 12px; line-height: 1.5em; word-break: break-all; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);"><code style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; border-radius: 3px; border: 0px; display: block; overflow-y: auto;">lighten(<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#cc3, 10%)// #d6d65c</span>
darken(<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#cc3, 10%)//#a3a329</span>
grayscale(<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#cc3) // #808080</span>
complement(<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">#cc3) // #33c</span></code><div><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><br></span></div></pre></div></div><p></p>
页:
[1]