scss在css基础语法上面增加了变量 (variables)、嵌套 (nested rules)、混合 (mixins)、导入 (inline imports) 等高级功能,使用scss可以很方便的提高开发效率 scss语法以.scss文件后缀结尾,其中语法格式有两种sass,scss,两种语法在书写风格有差异,如下代码所示
.scss
sass
scss
.container { width: 100px; height: 100%; .nav { width: 100px; }}
.container {
width: 100px;
height: 100%;
.nav {
}
.container width: 100px; height: 100%; .nav width: 100px;
.container
.nav
(常用)
scss允许将一套css样式嵌入另一套样式中,外层的容器将作为内层容器的父选择器,如下代码
.container { width: 500px; height: 100px; header { width: 100%; height: 20%; } main { width: 100%; height: 20%; } footer { width: 100%; height: 20%; }}
width: 500px;
height: 100px;
header {
width: 100%;
height: 20%;
main {
footer {
编译后
.container { width: 500px; height: 100px;}.container header { width: 100%; height: 20%;}.container main { width: 100%; height: 20%;}.container footer { width: 100%; height: 20%;}
.container header {
.container main {
.container footer {
有时需要在内层样式内选择外层的父元素,那么就可以使用&符号,如下代码所示
&
.container { width: 500px; height: 100px; &_header { width: 100%; height: 20%; &:hover { color: red($color: #000000); } } &_main { width: 100%; height: 20%; &:disabled { color: red; } } &_footer { width: 100%; height: 20%; &::after { position: absolute; content: ''; } }}
&_header {
&:hover {
color: red($color: #000000);
&_main {
&:disabled {
color: red;
&_footer {
&::after {
position: absolute;
content: '';
.container { width: 500px; height: 100px;}.container_header { width: 100%; height: 20%;}.container_header:hover { color: 0;}.container_main { width: 100%; height: 20%;}.container_main:disabled { color: red;}.container_footer { width: 100%; height: 20%;}.container_footer::after { position: absolute; content: '';}
.container_header {
.container_header:hover {
color: 0;
.container_main {
.container_main:disabled {
.container_footer {
.container_footer::after {
(不常用)
.container { width: 500px; height: 100px; font: { family: fantasy; size: 30em; weight: bold; } background: { image: url('xxx'); size: 100%; }}
font: {
family: fantasy;
size: 30em;
weight: bold;
background: {
image: url('xxx');
size: 100%;
.container { width: 500px; height: 100px; font-family: fantasy; font-size: 30em; font-weight: bold; background-image: url('xxx'); background-size: 100%;}
font-family: fantasy;
font-size: 30em;
font-weight: bold;
background-image: url('xxx');
background-size: 100%;
scss中使用$符号定义变量
$
$font-color: red;$font-size: 18px;$font-size-base: $font-size;.text { color: $font-color; font-size: $font-size;}span { font-size: $font-size-base;}
$font-color: red;
$font-size: 18px;
$font-size-base: $font-size;
.text {
color: $font-color;
font-size: $font-size;
span {
font-size: $font-size-base;
.text { color: red; font-size: 18px;}span { font-size: 18px;}
font-size: 18px;
.text { $font-color: red; $font-size: 18px; $font-size-base: $font-size; h1 { color: $font-color; font-size: $font-size; span { color: $font-color; font-size: $font-size; } }}
h1 {
.text h1 { color: red; font-size: 18px;}.text h1 span { color: red; font-size: 18px;}
.text h1 {
.text h1 span {
scss中支持+ - * /运算
+
-
*
/
$base-width: 10;$small-width: 30;$large-width: $base-width + $small-width;.div { width: $large-width + px;}.div1 { width: $small-width - $base-width + px;}.div2 { width: $small-width * $base-width + px;}.div2 { width: calc($small-width / $base-width) + px;}
$base-width: 10;
$small-width: 30;
$large-width: $base-width + $small-width;
.div {
width: $large-width + px;
.div1 {
width: $small-width - $base-width + px;
.div2 {
width: $small-width * $base-width + px;
width: calc($small-width / $base-width) + px;
.div { width: 40px;}.div1 { width: 20px;}.div2 { width: 300px;}.div2 { width: 3px;}
width: 40px;
width: 20px;
width: 300px;
width: 3px;
scss允许使用@extend集成其他样式规则
@extend
.item { width: 100%; height: 20%; background-color: red;}.item-1 { @extend .item; border: 1px solid blue;}.item-2 { @extend .item; border: 2px solid blue;}
.item {
background-color: red;
.item-1 {
@extend .item;
border: 1px solid blue;
.item-2 {
border: 2px solid blue;
.item,.item-2,.item-1 { width: 100%; height: 20%; background-color: red;}.item-1 { border: 1px solid blue;}.item-2 { border: 2px solid blue;}
.item,
.item-2,
当条件满足时,输入对应的样式
p { @if 1 + 1 == 2 { border: 1px solid; } @if 5 < 3 { border: 2px dotted; } @if null { border: 3px double; }}$type: monster;p { @if $type == ocean { color: blue; } @else if $type == matador { color: red; } @else if $type == monster { color: green; } @else { color: black; }}
p {
@if 1 + 1 == 2 {
border: 1px solid;
@if 5 < 3 {
border: 2px dotted;
@if null {
border: 3px double;
$type: monster;
@if $type == ocean {
color: blue;
} @else if $type == matador {
} @else if $type == monster {
color: green;
} @else {
color: black;
p { border: 1px solid;}p { color: green;}
@for $var from <start> through <end>
@for $i from 1 through 3 { .item-#{$i} { width: 2em * $i; }}
@for $i from 1 through 3 {
.item-#{$i} {
width: 2em * $i;
.item-1 { width: 2em;}.item-2 { width: 4em;}.item-3 { width: 6em;}
width: 2em;
width: 4em;
.item-3 {
width: 6em;
@for $var from <start> to <end>
@for $i from 1 to 3 { .item-#{$i} { width: 2em * $i; }}
@for $i from 1 to 3 {
.item-1 { width: 2em;}.item-2 { width: 4em;}
原文链接:https://www.cnblogs.com/jwyblogs/p/17027385.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728