在线编译 https://wow.techbrood.com/fiddle/11143
群组选择器的嵌套【编译前】
.container { h1, h2, h3 {margin-bottom: .8em} }
编译后
.container h1, .container h2, .container h3 { margin-bottom: .8em }
1. 优先使用scss中的变量
$biancolor:#ccc; #deno{ color:$biancolor }
说明
在scss中使用变量用$符号开头;
为什么我们要使用scss中的变量。
比如我们需要动态去切换主题的颜色,
我们就非常有必要去使用变量了哈。
这个虽然大家都会,但是在项目中就不一定回去使用变量了。
2.嵌套伪类:通过"&"实现 在中使用的":"符号 添加& 字符号
#deno{ .deo1{ background-color: #ccc; &:hover{ color:red } } }
编译后的
#deno .deo1 { background-color: #ccc; } #deno .deo1:hover { color: red; }
3. @mixin 和 @include的使用
@mixin 和 @include在项目中真的是超好用
当我们某一段css样式大量重复的时候,我们就可以了。
如果说:我们自定义的滚动条
编译前
//使用@mixin来定义共同的样式 @mixin test{ width:100px; height:100px; background:red } // @include 来使用 #deno{ @include test() }
编译后
#deno { width: 100px; height: 100px; background: red; }
4.传参 编译前
使用传递会让我们的css代码更加的灵活。
这样写起来会更加的高效
@mixin test($w,$h, $color){ width:$w; height:$h; background:$color }; #deno{ @include test(200px,200px,pink); }
编译后
#deno { width: 200px; height: 200px; background: pink; }
5. 默认传参
@mixin test($w:100px,$h:100px){ width:$w; height:$h; background-color:red; } #deno{ @include test(200px,200px); }
默认参数 编译前
<!-- 默认参数优先放置在最后 --> @mixin test($color,$size: 20px ) { background: $color; font-size: $size; } #deno { @include test( $color: green ) }
编译后
#deno { background: green; font-size: 20px; }
传参(指定参数) 这一种很有用
@mixin test($color,$size: 20px ,$w:100px ) { background: $color; font-size: $size; width:$w } #deno { @include test( $color: green, $w:200px ) }
条件判断
@mixin xingzhaung($x){ @if($x==zhengfang){ width:200px; height:200px; background: #000; }@else{ width:100px; height:200px; background: pink; } } div { @include xingzhaung(zhengfang) }
使用条件判断的时候,需要注意几点。
第一点就是需要添加 @符号
第二是: @mixin(函数方法理解为) 要在 @include(方法调用)之前
他们的位置千万不要颠倒了
否者会出问题,这个是第一个
条件语句的应用
比如说:滚动条样似的修改
超出几行隐藏显示省略号
字体大小、颜色这些都可以封装成为一个函数,直接使用就ok了哈
个人理解
在项目中,个人非常推荐去使用scss
scss在项目中最主要的就是这几个,但是很常用
如果小伙伴们在scss中发现了其他有趣的东西,欢迎评论