Sass Modules

本文最后更新于:2021年2月19日 晚上

Sass Modules

You don’t have to write all your Sass in a single file. You can split it up however you want with the @use rule. This rule loads another Sass file as a module, which means you can refer to its variables, mixins, and functions in your Sass file with a namespace based on the filename. Using a file will also include the CSS it generates in your compiled output!

SCSS SYNTAX

  • _base.scss
1
2
3
4
5
6
7
$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

body {
font: 100% $font-stack;
color: $primary-color;
}
  • styles.scss
1
2
3
4
5
6
@use 'base';

.inverse {
background-color: base.$primary-color;
color: white;
}

CSS OUTPUT

1
2
3
4
5
6
7
8
9
body {
font: 100% Helvetica, sans-serif;
color: #333;
}

.inverse {
background-color: #333;
color: white;
}

Notice we’re using @use 'base'; in the styles.scss file. When you use a file you don’t need to include the file extension. Sass is smart and will figure it out for you.


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!