SASS(SCSS)

官网
官方文档

认识 sass

Sass is an extension of CSS that adds power and elegance to the basic language. It allows you to use variables, nested rules, mixins, inline imports, and more, all with a fully CSS-compatible syntax. Sass helps keep large stylesheets well-organized, and get small stylesheets up and running quickly, particularly with the help of the Compass style library.

特点

  • Fully CSS-compatible
  • Language extensions such as variables, nesting, and mixins
  • Many useful functions for manipulating colors and other values
  • Advanced features like control directives for libraries
  • Well-formatted, customizable output

SASS 与 SCSS 转化

# Convert Sass to SCSS
$ sass-convert style.sass style.scss

# Convert SCSS to Sass
$ sass-convert style.scss style.sass

sass 安装

gem 安装

gem install sass

源码安装

git clone git://github.com/nex3/sass.git
cd sass
rake install

常见命令

# 编译一次
sass ./test.scss ./test.css
sass ./test.scss:./test.css
# 即时编译,即文件发生变化就重新编译
sass --watch ./test.scss:./test.css
# 编译整个文件夹
sass --watch app/sass:public/stylesheets
# 编译风格
sass --style expanded ./test.scss:./test.css

--style 可以跟以下几种参数:

nested :嵌套缩进的css代码,默认
expanded :展开的多行css代码
compact :简洁格式的css代码
compressed :压缩后的css代码

语法

声明变量

$width:200px;

默认变量

会被不带 !default 的覆盖

$brand-primary : darken(#428bca, 6.5%) !default; // #337ab7
$btn-primary-color: #fff !default;
$btn-primary-bg : $brand-primary !default;
$btn-primary-border : darken($btn-primary-bg, 5%) !default;

局部变量和全局变量

$color: orange !default;//定义全局变量(在选择器、函数、混合宏...的外面定义的变量为全局变量)
.block {
  color: $color;//调用全局变量
}
em {
  $color: red;//定义局部变量
  a {
    color: $color;//调用局部变量
  }
}
span {
  color: $color;//调用全局变量
}

编译结果

.block {
  color: orange;
}
em a {
  color: red;
}
span {
  color: orange;
}

Referencing Parent Selectors: &

Sometimes it's useful to use a nested rule's parent selector in other ways than the default. For instance, you might want to have special styles for when that selector is hovered over or for when the body element has a certain class. In these cases, you can explicitly specify where the parent selector should be inserted using the & character. For example:

a {
  font-weight: bold;
  text-decoration: none;
  &:hover { text-decoration: underline; }
  body.firefox & { font-weight: normal; }
}

编译结果

a {
  font-weight: bold;
  text-decoration: none; }
  a:hover {
    text-decoration: underline; }
  body.firefox a {
    font-weight: normal; }

嵌套

选择器嵌套

<!-- html 结构 -->
<header>
<nav>
    <a href=“##”>Home</a>
    <a href=“##”>About</a>
    <a href=“##”>Blog</a>
</nav>
<header>

sass &

nav {
  a {
    color: red;

    header & {
      color:green;
    }
  }  
}

编译结果

nav a {
  color:red;
}

header nav a {
  color:green;
}

属性嵌套

后面带一个冒号

.box {
  border: {
   top: 1px solid red;
   bottom: 1px solid green;
  }
}

编译结果

.box {
    border-top: 1px solid red;
    border-bottom: 1px solid green;
}

伪类嵌套

.clearfix{
  &:before,
  &:after {
    content:"";
    display: table;
  }
  &:after {
    clear:both;
    overflow: hidden;
  }
}

编译结果

.clearfix:before, .clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
  overflow: hidden;
}

混合宏 mixin

声明与调用

@mixin border-radius{
    -webkit-border-radius: 5px;
    border-radius: 5px;
}

button {
    @include border-radius;
}

不带任何值

@mixin border-radius($radius){
  -webkit-border-radius: $radius;
  border-radius: $radius;
}
.box {
  @include border-radius(3px);
}

编译结果

.box {
  -webkit-border-radius: 3px;
  border-radius: 3px;
}

带默认值

️使用时不带参数
@mixin border-radius($radius:3px) {
  -webkit-border-radius: $radius;
  border-radius: $radius;
}

.btn {
  @include border-radius;
}

编译结果

.btn {
  -webkit-border-radius: 3px;
  border-radius: 3px;
}
️使用时带参数
@mixin border-radius($radius:3px) {
  -webkit-border-radius: $radius;
  border-radius: $radius;
}

.box {
  @include border-radius(50%);
}

编译结果

.box {
  -webkit-border-radius: 50%;
  border-radius: 50%;
}

多个参数

@mixin center($width,$height){
  width: $width;
  height: $height;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -($height) / 2;
  margin-left: -($width) / 2;
}

.box-center {
  @include center(500px,300px);
}

编译结果

.box-center {
  width: 500px;
  height: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -150px;
  margin-left: -250px;
}
特别参数 ...
@mixin box-shadow($shadows...){
  @if length($shadows) >= 1 {
    -webkit-box-shadow: $shadows;
    box-shadow: $shadows;
  } @else {
    $shadows: 0 0 2px rgba(#000,.25);
    -webkit-box-shadow: $shadow;
    box-shadow: $shadow;
  }
}

.box {
  @include box-shadow(0 0 1px rgba(#000,.5),0 0 2px rgba(#000,.2));
}

编译结果

.box {
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
}

扩展/继承 @extend

//SCSS
.btn {
  border: 1px solid #ccc;
  padding: 6px 10px;
  font-size: 14px;
}

.btn-primary {
  background-color: #f36;
  color: #fff;
  @extend .btn;
}

.btn-second {
  background-color: orange;
  color: #fff;
  @extend .btn;
}

编译结果

//CSS
.btn, .btn-primary, .btn-second {
  border: 1px solid #ccc;
  padding: 6px 10px;
  font-size: 14px;
}

.btn-primary {
  background-color: #f36;
  color: #fff;
}

.btn-second {
  background-clor: orange;
  color: #fff;
}

占位符 %placeholder

%placeholder 声明的代码,如果不被 @extend 调用的话,不会产生任何代码

//SCSS
%mt5 {
  margin-top: 5px;
}
%pt5{
  padding-top: 5px;
}

.btn {
  @extend %mt5;
  @extend %pt5;
}

.block {
  @extend %mt5;

  span {
    @extend %pt5;
  }
}

编译结果

//CSS
.btn, .block {
  margin-top: 5px;
}

.btn, .block span {
  padding-top: 5px;
}

从编译出来的 CSS 代码可以看出,通过 @extend 调用的占位符,编译出来的代码会将相同的代码合并在一起。

混合宏 vs 继承 vs 占位符

总结

建议:

  • 如果你的代码块中涉及到变量,建议使用混合宏来创建相同的代码块。
  • 如果你的代码块不需要专任何变量参数,而且有一个基类已在文件中存在,那么建议使用 Sass 的继承。
  • 否则优先使用占位符。

@at-root

.parent {
  ...
  @at-root {
    .child1 { ... }
    .child2 { ... }
  }
  .step-child { ... }
}

编译结果

.parent { ... }
.child1 { ... }
.child2 { ... }
.parent .step-child { ... }

插值

#{}

变量和属性

$properties: (margin, padding);
@mixin set-value($side, $value) {
    @each $prop in $properties {
        #{$prop}-#{$side}: $value;
    }
}
.login-box {
    @include set-value(top, 14px);
}

编译结果

.login-box {
    margin-top: 14px;
    padding-top: 14px;
}

选择器

@mixin generate-sizes($class, $small, $medium, $big) {
    .#{$class}-small { font-size: $small; }
    .#{$class}-medium { font-size: $medium; }
    .#{$class}-big { font-size: $big; }
}
@include generate-sizes("header-text", 12px, 20px, 40px);

编译结果

.header-text-small { font-size: 12px; }
.header-text-medium { font-size: 20px; }
.header-text-big { font-size: 40px; }

更多请查看慕课网

注释

  • 类似 CSS 的注释方式,使用 ”/* ”开头,结属使用 ”*/ ”,会在编译出来的 CSS 显示
  • 类似 JavaScript 的注释方式,使用“//”,不会在编译出来的 CSS 显示

数据类型

  • numbers (e.g. 1.2, 13, 10px)
  • strings of text, with and without quotes (e.g. "foo", 'bar', baz)
  • colors (e.g. blue, #04a3f9, rgba(255, 0, 0, 0.5))
  • booleans (e.g. true, false)
  • nulls (e.g. null)
  • lists of values, separated by spaces or commas (e.g. 1.5em 1em 0 2em, Helvetica, Arial, sans-serif)
  • maps from one value to another (e.g. (key1: value1, key2: value2))
  • function references

两种字符串类型:

  • 有引号字符串 (quoted strings),如 "Lucida Grande" 、'http://sass-lang.com'
  • 无引号字符串 (unquoted strings),如 sans-serifbold。

在编译 CSS 文件时不会改变其类型。只有一种情况例外,使用 #{ }插值语句 (interpolation) 时,有引号字符串将被编译为无引号字符串,这样方便了在混合指令 (mixin) 中引用选择器名。

@mixin firefox-message($selector) {
  body.firefox #{$selector}:before {
    content: "Hi, Firefox users!";
  }
}
@include firefox-message(".header");

编译结果

body.firefox .header:before {
  content: "Hi, Firefox users!"; 
}

逻辑运算

@mixin col-sm($width: 50%){
    @if type-of($width) != number {
        @error "$width 必须是一个数值类型,你输入的 width 是: #{$width}.";
    }
    @if not unitless($width){
        @if unit($width) != "%" {
            @error "$width 应该是一个百分值,你输入的 width 是: #{$width}.";
        }
    } @else {
        @warn "$width 应该是一个百分值,你输入的 width 是: #{$width}.";
        $width: percentage($width);
    }

    @media (min-width:768px){
        width:$width;
    }
}

.section{
    @include col-sm(0.2);
}

文章中知识点整理自

官方文档
资料1:慕课网 Sass入门篇
资料2:慕课网 Sass和Compass必备技能之Sass篇

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

推荐阅读更多精彩内容

  • 什么是SASS? SASS(Syntactically Awesome Stylesheets)是一种动态样式语言...
    桂圆_noble阅读 6,733评论 0 15
  • Scss 是Sass的新语法,从外形上看它和CSS长的几乎一模一样。文件名以.scss为扩展名。 一.Sass编译...
    芹菜斯_嘉丽阅读 375评论 0 0
  • 大学的团支书 一个让我觉得尴尬又十分羡慕的女孩子 她很上进 很好强 用力过度让我觉得做作和有点不能忍受 比如 外教...
    唐斯彤阅读 202评论 0 1
  • 为人父母您望子成龙、望女成凤没有错,但您不应该把自己的想法强加在孩子身上,更不应该不讲道理的强迫子女去遵从。您所谓...
    伞心悦目阅读 2,847评论 1 0
  • 透明状态栏和导航栏的终极解决方案 郭神推荐:https://github.com/Zackratos/Ultima...
    xiny1024阅读 1,267评论 0 2