Sass 定義Mixin

@mixin指令用於定義混入,它包括任選的變數和參數中的mixin名稱後。

示例

下麵的例子演示了在SCSS檔使用@mixin:

sample.html

<html>
<head><meta charset="utf-8"> <title> Mixin示例-www.xuhuhu.com</title>
   <link rel="stylesheet" type="text/css" href="sample.css"/>
</head>
<body>
<div class="cont">
   <h1>Example using include</h1>
   <h3>Directive is used to define the Mixins, it includes variables and argument optionally.</h3>
</div>
</body>
</html>
接下來,創建檔 sample.scss。

sample.scss

@mixin style {
.cont{
	color: #77C1EF;
    }
}
@include style;
可以告訴SASS監視檔,並隨時使用下麵的命令更新SASS檔來修改CSS:
sass --watch C:\Ruby22-x64\sample.scss:sample.css
接著執行上面的命令,它會自動創建 sample.css 檔,下麵的代碼:

sample.css

.cont {
  color: #77C1EF;
  }

輸出結果

讓我們來執行以下步驟,看看上面的代碼工作:
保存上面的html代碼在 sample.html 檔中。
在流覽器中打開該HTML檔,輸出如下得到顯示。


上一篇: Sass控制指令&運算式 下一篇: Sass包含mixin