Sass 包含Mixin

2018-12-30 15:54 更新

描述

@include 指令用于在文档中包含mixin。将使用mixin的名称,并将可选参数传递给它。由mixin定义的样式可以包含在当前规则中。

例子

下面的例子演示了如何在SCSS文件中使用mixin:

sample.html

<html>
<head>
   <title> Mixin example of sass</title>
   <link rel="stylesheet" type="text/css" href="sample.css"/>
</head>
<body>
<div class="cont">
   <h2>Example using include</h2>
   <h3>Different Colors</h3>
   <ul>
   	<li>Red</li>
   	<li>Green</li>
   	<li>Blue</li>
   </ul>
  </div>
</body>
</html>

接下来,创建文件 sample.scss

sample.scss

@mixin style {
.cont{
	background-color: #77C1EF;
	color: #ffffff;
    }
h3 {
	color: #ffffff;
	}
}
@include style;

您可以通过使用以下命令让SASS查看文件并在SASS文件更改时更新CSS:

sass --watch C:\ruby\lib\sass\sample.scss:sample.css

接下来执行上面的命令,它将自动创建 sample.css 文件,代码如下:

sample.css

.cont {
  background-color: #77C1EF;
  color: #ffffff;
 }

h3 {
  color: #ffffff;
 }

输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上述html代码保存在 sample.htm 文件中。

  • 在浏览器中打开此HTML文件,将显示如下输出。

Sass rules and directives
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号