Sass 简介
Sass 安装
Sass 变量
Sass Nested Rules and Properties
Sass @import and Partials
Sass @mixin and @include
Sass @extend and Inheritance
Sass 函数
Sass String
Sass Numeric
Sass List
Sass Map
Sass Selector
Sass Introspection
Sass Color
Sass @mixin and @include - SASS基础教程 - 笔下光年
网站首页
Sass @mixin and @include
## Sass Mixins The `@mixin` directive lets you create CSS code that is to be reused throughout the website. The `@include` directive is created to let you use (include) the mixin. ## Defining a Mixin A mixin is defined with the `@mixin` directive. Sass @mixin Syntax: ```sass @mixin name { property: value; property: value; ... } ``` The following example creates a mixin named "important-text": SCSS Syntax: ```sass @mixin important-text { color: red; font-size: 25px; font-weight: bold; border: 1px solid blue; } ``` <div class="callout callout-warning mb-3">Tip: A tip on hyphens and underscore in Sass: Hyphens and underscores are considered to be the same. This means that @mixin important-text { } and @mixin important_text { } are considered as the same mixin!</div> ## Using a Mixin The `@include` directive is used to include a mixin. Sass @include mixin Syntax: ```sass selector { @include mixin-name; } ``` So, to include the important-text mixin created above: SCSS Syntax: ```sass .danger { @include important-text; background-color: green; } ``` The Sass transpiler will convert the above to normal CSS: CSS output: ```sass .danger { color: red; font-size: 25px; font-weight: bold; border: 1px solid blue; background-color: green; } ``` A mixin can also include other mixins: SCSS Syntax: ```sass @mixin special-text { @include important-text; @include link; @include special-border; } ``` ## Passing Variables to a Mixin Mixins accept arguments. This way you can pass variables to a mixin. Here is how to define a mixin with arguments: SCSS Syntax: ```sass /* Define mixin with two arguments */ @mixin bordered($color, $width) { border: $width solid $color; } .myArticle { @include bordered(blue, 1px); // Call mixin with two values } .myNotes { @include bordered(red, 2px); // Call mixin with two values } ``` Notice that the arguments are set as variables and then used as the values (color and width) of the border property. After compilation, the CSS will look like this: CSS Output: ```sass .myArticle { border: 1px solid blue; } .myNotes { border: 2px solid red; } ``` ## Default Values for a Mixin It is also possible to define default values for mixin variables: SCSS Syntax: ```sass @mixin bordered($color: blue, $width: 1px) { border: $width solid $color; } ``` Then, you only need to specify the values that change when you include the mixin: SCSS Syntax: ```sass .myTips { @include bordered($color: orange); } ``` ## Using a Mixin For Vendor Prefixes Another good use of a mixin is for vendor prefixes. Here is an example for transform: SCSS Syntax: ```sass @mixin transform($property) { -webkit-transform: $property; -ms-transform: $property; transform: $property; } .myBox { @include transform(rotate(20deg)); } ``` After compilation, the CSS will look like this: CSS Output: ```sass .myBox { -webkit-transform: rotate(20deg); -ms-transform: rotate(20deg); transform: rotate(20deg); } ```
上一篇:
Sass @import and Partials
下一篇:
Sass @extend and Inheritance