freeCodeCamp/guide/chinese/certifications/front-end-libraries/sass/create-reusable-css-with-mi.../index.md

1.2 KiB
Raw Blame History

title localeTitle
Create Reusable CSS with Mixins 使用Mixins创建可重用的CSS

使用Mixins创建可重用的CSS

Mixin是开发人员使用SASS而不是纯CSS的强大功能之一,因为它允许您在样式表中使用Function

要创建mixin您应该遵循以下方案

@mixin custom-mixin-name($param1, $param2, ....) { 
    // CSS Properties Here... 
 } 

要在元素中使用它,您应该使用@include后跟您的Mixin名称,如下所示:

element { 
    @include custom-mixin-name(value1, value2, ....); 
 } 

[示例]在SASS写入Text Shadow

**目标:**将自定义Text Shadow应用于h4元素

HTML


<h4>This text needs a Shadow!</h4> 

SASS 用SCSS语法编写

@mixin custom-text-shadow($offsetX, $offsetY, $blurRadius, $color) { 
    -webkit-text-shadow: $offsetX, $offsetY, $blurRadius, $color; 
    -moz-text-shadow: $offsetX, $offsetY, $blurRadius, $color; 
    -ms-text-shadow: $offsetX, $offsetY, $blurRadius, $color; 
    text-shadow: $offsetX, $offsetY, $blurRadius, $color; 
 } 
 h2 { 
    @include custom-text-shadow(1px, 3px, 5px, #999999) 
 }