Hellom's Studio.

scss 之定义通用样式

字数统计: 567阅读时长: 3 min
2020/04/13 Share

安装 scss

  • npm i -D sass sass-loader

VsCode 安装插件

  • px to rem

    • alt + z: 即可转换为 rem

    • 默认 1rem = 16px ,可在配置文件中根据实际情况修改 首选项 => 设置 => 搜索 px to rem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// _variables.scss
// 全局定义颜色
// colors
$colors:(
"priamry":#db9e3f,
"white":#fff,
"dark": #000,
"dark-1":#666,
"red":#e74c3c,
"info":#4b67af,
);

// font size
$base-font-size: 1rem;

$font-sizes:(xxs:0.6154,
xs:0.7692, //10px
sm:0.9231, //12px
md:1, //13px
lg:1.0769, //14px
xl:1.2308, //16px
);


// flex justify-content
$flex-jc:(start:flex-start,
end: flex-end,
center:center,
between:space-between,
around:space-around);

// flex aligin-item
$flex-ai:(start:flex-start,
end: flex-end,
center:center,
baseline:baseline,
stretch:stretch);

// spacing
// 定义边距 0-5级
// .mt-1 => margin-top
$spacing-types:(m: margin,
p: padding);

// 定义方向
$spacing-directions:(t:top, r:right, b:bottom, l:left);
// 定义基础尺寸
$spacing-base-size:1rem;

// 定义倍数
$spacing-sizing:(0:0,
1:0.25,
2:0.5,
3:1,
4:1.5,
5:3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// style.scss

@import './variables.scss';

* {
box-sizing: border-box;
outline: none;
}

html {
font-size: 13px;
}

body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}

// colors => .text-priamry{color: #db9e3f}
@each $colorKey,
$color in $colors {
.text-#{$colorKey} {
color: $color !important;
}

.bg-#{$colorKey} {
background-color: $color !important;
}
}


// text-align
@each $var in (left, center, right) {
.text-#{$var} {
text-align: $var !important;
}
}

// font size => .fs-md{font-size:1rem}
@each $sizeKey,
$size in $font-sizes {
.fs-#{$sizeKey} {
font-size: $size * $base-font-size;
}
}

// flex
.d-flex {
display: flex;
}

.flex-column {
flex-direction: column;
}

// flex justify-content
@each $key,
$value in $flex-jc {
.jc-#{$key} {
justify-content: $value;
}
}

// flex aligin-item
@each $key,
$value in $flex-ai {
.ai-#{$key} {
align-items: $value;
}
}

.flex-1 {
flex: 1;
}


// spacing
@each $typeKey,
$type in $spacing-types {

// .m-1
@each $sizingKey,
$sizing in $spacing-sizing {

.#{$typeKey}-#{$sizingKey} {

#{$type}: $sizing * $spacing-base-size;
}
}

// .mx-1 .my-1
@each $sizingKey,
$sizing in $spacing-sizing {

.#{$typeKey}x-#{$sizingKey} {

#{$type}-left: $sizing * $spacing-base-size;
#{$type}-right: $sizing * $spacing-base-size;
}

.#{$typeKey}y-#{$sizingKey} {

#{$type}-top: $sizing * $spacing-base-size;
#{$type}-bottom: $sizing * $spacing-base-size;
}
}

// .mt-1 { margin-top: 0.25 }
@each $dirKey,
$dir in $spacing-directions {

@each $sizingKey,
$sizing in $spacing-sizing {

.#{$typeKey}#{$dirKey}-#{$sizingKey} {

#{$type}-#{$dir}: $sizing * $spacing-base-size
}
}
}
}

// button
.btn {
border: none;
border-radius: 0.1538rem;
font-size: map-get($map: $font-sizes, $key: 'sm') * $base-font-size;
padding: 0.2rem 0.6rem;
}

// width height
.w-100 {
width: 100%;
}

.h-100 {
height: 100%;
}
CATALOG
  1. 1. 安装 scss
  2. 2. VsCode 安装插件