SCSS语法

嵌套规则

// scss
#id {
    color: red;
    .name {
        color: blue;
        .child {
            color: yellow;
        }
    }
}
// css
#id {
    color: red;
}

#id .name {
    color: blue;
}

#id .name .child {
    color: yellow;
}

引用父选择器 &

// scss
.btn {
    background-color: #fff;
    &.active {
        background-color: red;
    }
    &:hover {
        background-color: blue;
    }
    &:visited {
        background-color: yellow;
    }
    &-success {
        background-color: pink;
    }
}
// css
.btn {
    background-color: #fff;
}

.btn.active {
    background-color: red;
}

.btn:hover {
    background-color: blue;
}

.btn:visited {
    background-color: yellow;
}

.btn-success {
    background-color: pink;
}

嵌套属性规则

// scss
.attr {
    font: {
        family: fantasy;
        size: 1.2em;
        line-hight: 1.4;
    };
    border: {
        radius: 20px;
        color: blue;
    };
}
// css
.attr {
    font-family: fantasy;
    font-size: 1.2em;
    font-line-hight: 1.4;
    border-radius: 20px;
    border-color: blue;
}

运算

声明变量

// scss
$width: 50px;

.wt {
    width: $width;
}
// css
.wt {
    width: 50px;
}

数学运算

// scss
#id {
    width: (1 + 2) *3px;
}
// css
#id {
    width: 9px;
}

特殊的 / 除法运算符

// scss
p {
  font: 10px/8px;             // 纯 CSS,不是除法运算
  $width: 1000px;
  width: $width/2;            // 使用了变量,是除法运算
  width: round(1.5px)/2;        // 使用了函数,是除法运算
  height: (500px/2);          // 使用了圆括号,是除法运算
  margin-left: 5px + 8px/2px; // 使用了加(+)号,是除法运算
  padding-left: + 100px / 2;  
}
// css
p {
    font: 10px/8px;
    width: 500px;
    width: 1px;
    height: 250px;
    margin-left: 9px;
    padding-left: 50px;
}

scss为了兼容IE8,10px/8px不能种的 /不能编译为除法运算符,可以在除法运算前使用+运算符创建命名空间

颜色运算符

// scss
p {
  color: #001100 + #040506;
}
p {
  color: #010 + #040506;
}
// css
p {
    color: #041606;
}

p {
    color: #041606;
}

插值 #{}

// scss
$name: foo;
$attr: border;
p.#{$name} {
  #{$attr}-color: blue;
}

p {
  $font-size: 12px;
  $line-height: 30px;
  font: #{$font-size}/#{$line-height};
}
// css
p.foo {
    border-color: blue;
}

p {
    font: 12px/30px;
}

插值内的计算,null为空字符串

// scss
p:before {
    content: 'string #{1+2} str';
}
$val: null;
p:before {
    content: 'sting #{$val} str';
}
// css
p:before {
    content: "string 3 str";
}

p:before {
    content: "sting  str";
}

SCSS 指令

import导入

import 可以导入style.scss的样式到当前文件下

// scss
@import 'style.scss';

import 指令支持嵌套

// scss
.warp {
  @import 'style.scss';
}

media 媒体查询

// scss
$media: screen;
$feature: -webkit-min-device-pixel-ratio;
$value: 1.5;
.father{
    .sidebar {
        width: 800px;
    }
    @media #{$media} and ($feature: $value) {
      height: 300px;
      .sidebar {
        width: 500px;
      }
      .hello {
          color: red;
      }
    }
}
// css
.father .sidebar {
    width: 800px;
}

@media screen and (-webkit-min-device-pixel-ratio: 1.5) {
    .father {
        height: 300px;
    }

    .father .sidebar {
        width: 500px;
    }

    .father .hello {
        color: red;
    }
}

extend 继承

extend 只能继承选择器

// scss
.error {
  border: 1px #f00;
  background-color: #fdd;
  &.intrusion {
      background-image: url("/image/hacked.png");
    }
}

.seriousError {
  @extend .error;
  border-width: 3px;
}
// css
.error, .seriousError {
    border: 1px #f00;
    background-color: #fdd;
}

.error.intrusion, .intrusion.seriousError {
    background-image: url("/image/hacked.png");
}

.seriousError {
    border-width: 3px;
}

extend继承选择器的相关性

// scss
.hoverlink {
  @extend a:hover;
}

a:hover {
  text-decoration: underline;
}

.comment a.user:hover { // a.user:hover 和a:hover 有相关性,同样被继承 
  font-weight: bold;
}

.comment a .user:hover { // a .user:hover 与a:hover 无相关性,不被继承
  color: red;
}
// css
a:hover, .hoverlink {
    text-decoration: underline;
}

.comment a.user:hover, .comment .user.hoverlink {
    font-weight: bold;
}

.comment a .user:hover {
    color: red;
}

extend的多次继承

// scss
.error {
  border: 1px #f00;
  background-color: #fdd;
}
.attention {
  font-size: 3em;
  background-color: #ff0;
}
.seriousError {
  @extend .error;
  @extend .attention;
  border-width: 3px;
}
// csss
.error, .seriousError {
    border: 1px #f00;
    background-color: #fdd;
}

.attention, .seriousError {
    font-size: 3em;
    background-color: #ff0;
}

.seriousError {
    border-width: 3px;
}

extend的链式继承

// scss
.error {
  border: 1px #f00;
  background-color: #fdd;
}
.seriousError {
  @extend .error;
  border-width: 3px;
}
.criticalError {
  @extend .seriousError;
  position: fixed;
  top: 10%;
  bottom: 10%;
  left: 10%;
  right: 10%;
}
// css
.error, .seriousError, .criticalError {
    border: 1px #f00;
    background-color: #fdd;
}

.seriousError, .criticalError {
    border-width: 3px;
}

.criticalError {
    position: fixed;
    top: 10%;
    bottom: 10%;
    left: 10%;
    right: 10%;
}

mixin + include混合

mixin 混合属性

// scss
@mixin large-text {
  font: {
    family: Arial;
    size: 20px;
    weight: bold;
  }
  color: #ff0000;
}

.page-title {
  @include large-text;
  padding: 4px;
  margin-top: 10px;
}
// css
.page-title {
    font-family: Arial;
    font-size: 20px;
    font-weight: bold;
    color: #ff0000;
    padding: 4px;
    margin-top: 10px;
}

mixin 在外层混合, 不依赖于父层结构

// scss
@mixin silly-links {
  a {
    color: blue;
    background-color: red;
  }
}

@include silly-links;
// css
a {
    color: blue;
    background-color: red;
}

mixin 设置参数

// scss
@mixin sexy-border($color, $width) {
  border: {
    color: $color;
    width: $width;
    style: dashed;
  }
}

p { @include sexy-border(blue, 1in); }
// css
p {
    border-color: blue;
    border-width: 1in;
    border-style: dashed;
}

mixin 混合未知格式和数量的变量, 使用arg...

// scss
@mixin box-shadow($shadows...) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}

.shadows {
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
// css
.shadows {
    -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
    -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
    box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

也可以使用插值编写,不过mixin和include指定的是一个不会进行动态识别的字面量不能使用插值

// scss
$b: box-shadow;
@mixin box-shadow($shadows...) {
  -moz-#{$b}: $shadows;
  -webkit-#{$b}: $shadows;
  #{$b}: $shadows;
}

.shadows {
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
// css
.shadows {
    -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
    -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
    box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

mixin 的复合 mixin内部也可以include

// scss
@mixin compound {
  @include highlighted-background;
  @include header-text;
}

@mixin highlighted-background { background-color: #fc0; }
@mixin header-text { font-size: 20px; }

.com {
    @include compound;
}
// css
.com {
    background-color: #fc0;
    font-size: 20px;
}

SCSS 流程控制指令

if 条件语句

// scss
p {
  @if 1 + 1 == 2 { border: 1px solid;  }
  @if 5 < 3      { border: 2px dotted; }
  @if null       { border: 3px double; }
}
// css
p {
    border: 1px solid;
}

else if 语句

// scss
$type: monster;
p {
  @if $type == ocean {
    color: blue;
  } @else if $type == matador {
    color: red;
  } @else if $type == monster {
    color: green;
  } @else {
    color: black;
  }
}
// css
p {
    color: green;
}

for 循环语句

// scss
@for $i from 1 through 3 {
  .item-#{$i} { width: 2em * $i; }
}
// css
.item-1 {
    width: 2em;
}

.item-2 {
    width: 4em;
}

.item-3 {
    width: 6em;
}

each 循环语句

// scss
@each $animal in puma, sea-slug, egret, salamander {
  .#{$animal}-icon {
    background-image: url('/images/#{$animal}.png');
  }
}
// css
.puma-icon {
    background-image: url("/images/puma.png");
}

.sea-slug-icon {
    background-image: url("/images/sea-slug.png");
}

.egret-icon {
    background-image: url("/images/egret.png");
}

.salamander-icon {
    background-image: url("/images/salamander.png");
}

while循环语句

// scss
$i: 6;
@while $i > 0 {
  .item-#{$i} { width: 2em * $i; }
  $i: $i - 2;
}
// css
.item-6 {
    width: 12em;
}

.item-4 {
    width: 8em;
}

.item-2 {
    width: 4em;
}

SCSS函数指令

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容

  • 基础 声明变量 普通变量 默认变量 变量覆盖:只需要在默认变量之前重新声明下变量即可 变量的调用 局部变量和全局变...
    Jill1231阅读 1,219评论 0 1
  • 1、SCSS 是 Sass 的新语法格式,从外形上来判断他和 CSS 长得几乎是一模一样,代码都包裹在一对大括号里...
    夜幕小草阅读 1,659评论 2 10
  • 再谈CSS 预处理器2016-09-09 Justineo JavaScript转自:http://efe.bai...
    抓住时间的尾巴吧阅读 1,472评论 0 2
  • Variables(声明变量) Nesting(嵌套) Partials Import(引入其他样式) Mixin...
    胡萝卜樱阅读 831评论 0 0
  • 1. CSS预处理器 定义了一种新的专门的编程语言,编译后成正常的CSS文件。为CSS增加一些编程的特性,无需考虑...
    恰皮阅读 105,870评论 20 105