sass总结

sass是css的辅助工具,它是在css的基础上新增了变量,嵌套,导入,混合,继承,运算,函数等功能,sass补缺了css在编程上的很多缺陷。

1.变量$

//声明变量
$value:12px;
html {
  font-size: $value;
}

编译后:
html {
  font-size: 12px;
}

2. 嵌套

(1)普通的嵌套

.wrapper {
   width: 100px;
   background: red;
   .inner {
      width: 50px;
      background: yellow;
   }
}

编译后:
.wrapper {
  width: 100px;
  background: red;
}
.wrapper .inner {
  width: 50px;
  background: yellow;
} 

(2)属性嵌套

a {
  font: {
    size: 12px;
    weight: 11;
  }
  color: yellow;
}

编译后

a {
  font-size: 12px;
  font-weight: 11;
  color: yellow;
}

3. 继承

(1)父选择器继承&

.wrapper {
  color: green;
  &:hover {
     color: red;
  }
}

编译后

.wrapper {
  color: green;
}
.wrapper:hover {
  color: red;
}

(2)继承其他选择器属性@extend

继承按照选择器划分可分为:简单继承、复杂继承、多重继承、连续继承、选择器列继承、占位符继承。

注意:继承找不到继承的选择器会报错,可在选择器后面加!optional;@media内部不能继承外部选择器的属性。

// 简单继承
.parent {
  width: 100px;
  height: 20px;
  color: yellow;
}
.child {
  @extend .parent
}

编译后
.parent, .child {
  width: 100px;
  height: 20px;
  color: yellow;
}

//复杂选择器继承
.link {
  @extend a:hover;
}
.wrapper a.inner:hover {
  color: green;
}

编译后
.wrapper .link.inner, .wrapper a.inner:hover {
  color: green;
}

//多重继承
.inner {
  width: 12px;
  height: 20px;
  color: red;
}
.content {
  font-size: 12px;
  color: yellow;
}
.wrapper {
  @extend .inner;
  @extend .content;
  color: yellow;
}

编译后
.inner, .wrapper {
  width: 12px;
  height: 20px;
  color: red;
}
.content, .wrapper {
  font-size: 12px;
  color: yellow;
}
.wrapper {
  color: yellow;
}

//连续继承
 .inner1 {
  width: 12px;
}
.inner2 {
  @extend .inner1;
  height: 100px;
}
.inner3 {
 @extend .inner2;
 color: green;
}

编译后
.inner1, .inner2, .inner3 {
  width: 12px;
}
.inner2, .inner3 {
  height: 100px;
}
.inner3 {
  color: green;
}

// 选择器列继承
.wrapper .inner {
 @extend .child;
}
.child {
 color: blue;
 &:hover {
  font-size: 12px;
 }
}

编译后
.child, .wrapper .inner {
  color: blue;
}
.child:hover, .wrapper .inner {
  font-size: 12px;
}

//占位符继承%(模板)
.wrapper a%placeholder {
 width: 100px;
  color: yellow;
}
.child {
  @extend %placeholder;
}

编译后
.wrapper a.child {
  width: 100px;
  color: yellow;
}

3.导入与媒体查询

(1)导入@import

sass引入三种形式:含有scss或sass; 没有文件后缀;引入多文件;文件名加_前缀,便不会编译该文件;@import可以在css模块中引进,但是不能在混合指令或者控制指令中引入@import。

//第一种
@import "foo.scss"或@import "foo.sass";
===========
//第二种
@import "foo";
===========
//第三种
@import "foo","info";
===========
//第四种
@import "_colors.scss";
//第五种
#main {
  @import ‘colors.scss‘;
}
##colors.scss
.examlp {
  color: red;
}

编译后
#main .examlp {
  color: red;
}

当引入以下四种形式时,@import仅作为普通的css语句。

  • 文件扩展名.css
  • 文件名以http开头
  • 文件名是url()
  • 文件名是media query
@import ‘foo.css‘;
@import ‘http://github.com/lidaylin/foo.scss‘;
@impore ‘foo‘ screen;
@import url()

编译后
@import ‘foo.css‘;
@import ‘http://github.com/lidaylin/foo.scss‘;
@impore ‘foo‘ screen;
@import url()

(2)媒体查询@media

@media用法基本与css一致,sass增加了嵌套的功能。大致有两种,一种是嵌套在选择器内部,编译后会包含嵌套的父选择器;另外一种是嵌套自身,@media会进行合并。

//被父选择器嵌套
.sider {
  width: 100px;
  @media screen and (orientation=portrait) {
    font-size: 12px;
  }
}

编译后
.sider {
  width: 100px;
}
@media screen and (orientation=portrait) {
  .sider {
    font-size: 12px;
  }
}

//嵌套自身
@media screen {
  @media (orientation=portait) {
    .sider {
      font-size: 18px;
    }
  }
}

编译后
@media screen and (orientation=portait) {
  .sider {
    font-size: 18px;
  }
}

4.混合

@mixin text {
  font: {
    family: arial;
    weight: bold;
    size: 12px;
  }
  color: #333;
}
.large-text {
  @include text;
  background: yellow;
}

编译后
.large-text {
  font-size: 12px;
  font-family: arial;
  font-weight: bold;
  color: #333;
}

以上是混合的常规定义(@mixin)和调用(@include),还可以用参数和向混合样式中导入内容。

$color: yellow;
$status: solid !default;
@mixin border-style1($color, $status){
  border-radius: 1px $status $color;
  -webkit-border-radius: 1px $status $color;
  -moz-border-radius: 1px $status $color;
  -ms-border-radius: 1px $status $color;
}

//关键词混合
@mixin border-style2($color, $status:dash){
  border-radius: 1px $status $color;
  -webkit-border-radius: 1px $status $color;
  -moz-border-radius: 1px $status $color;
  -ms-border-radius: 1px $status $color;
}
.edging1 {
  width: 120px;
  height: 20px;
  @include border-style2($color);
}
.edging2 {
  width: 120px;
  height: 20px;
  @include border-style1($color, $status);
}
编译后
.edging2 {
  width: 120px;
  height: 20px;
  border-radius: 1px solid yellow;
  -webkit-border-radius: 1px solid yellow;
  -moz-border-radius: 1px solid yellow;
  -ms-border-radius: 1px solid yellow;
}
.edging1 {
  width: 120px;
  height: 20px;
  border-radius: 1px dash yellow;
  -webkit-border-radius: 1px dash yellow;
  -moz-border-radius: 1px dash yellow;
  -ms-border-radius: 1px dash yellow;
}

//变量参数
$shadow: 10px 10px 5px #333;
$value: #333 #e1e1e1;
@mixin box-shadow($shadow...) {
  width: 100px;
  height: 50px;
  background: yellow;
  box-shadow: $shadow;
}
@mixin colors($textColor, $background) {
  color: $textColor;
  background: $background;
}
.show {
  @include box-shadow($shadow...);
}
.text {
 @include colors($value...);
}

编译后
.show {
  width: 100px;
  height: 50px;
  background: yellow;
  box-shadow: 10px 10px 5px #333;
}
.text {
  color: #333;
  backgrond: #e1e1e1;
}

//向混合样式中导入内容
////导入整块内容
@mixin wrapper {
  html {
   @content
  }
}
@include wrapper {
  .inner {
    margin: 0 auto;
  }
}

编译后
html .inner {
  margin: 0 auto;
}

////导入属性
@mixin wrapper($color: blue) {
  @content;
  color: $color;
}
.text {
  @include wrapper {margin: 0 auto;}
}

编译后
.text {
  margin: 0 auto;
  color: blue;
}

5. 控制指令

(1)条件判断语句 @if与@else

.text {
  @if 1+1 == 2 {
    border: 2px;
  } @else {
    border: 4px;
  }
}

(2) 循环语句(@for、@each、@while)

@for的取值范围两种表达方式,其一a though b,范围是a到b;其二a to b,范围是a到b-1。

@for $var in 1 though 3 {
  .item-#{$var} {
    font-size: 12px * $var;
  }
}
@for $var in 4 to 6 {
  .item-#{$var} {
    font-size: 6px * $var;
  }
}

编译后
.item-1 {
  font-size: 12px;
}
.itme-2 {
  font-size: 24px;
}
.itme-3 {
  font-size: 36px;
}
.itme-4 {
  font-size: 24px;
}
.item-5 {
  font-size: 30px;
}
*****没有6了*****

@each 取值范围的三种形式,散列、散列对象、键值对。

//散列
@each $var in safari, chrome, fix {
  .#{$var}-icon {
    background: url(‘./images/#{$var}.png‘);
  }
}

编译后
.safari-icon {
  background: url(‘./images/safari.png‘);
}
.chrome-icon {
  background: url(‘./images/chrome.png‘);
}
.fix-icon {
  background: url(‘./images/fix.png‘);
}

//散列对象
@each $color, $width, $height in (blue, 12px, 18px)
                                 (yellow, 20px, 32px)
{
  #{$color}-color {
    color: $color;
    width: $width;
    height: $height;
  }
}
编译后
blue-color {
  color: blue;
  width: 12px;
  height: 18px;
}
yellow-color {
  color: yellow;
  width: 20px;
  height: 32px;
}

//键值对
@each $key, $value in {w1: 12px, w2: 24px, w3: 36} {
  .#{$key} {
    width: $value;
  }
}

编译后
.w1 {
  width: 12px;
}
.w2 {
  width: 24px;
}
.w3 {
  width: 36px;
}

6. 函数

$height: 20px;
@function swicth-width($n) {
  @return $n * $height;
}
.wrapper {
  color: yellow;
  width: switch-width(2);
}

编译后
.wrapper {
  color: yellow;
  width: 40px
}

7. 运算

数字的加减乘除和颜色的计算。

//加减乘除.wrapper {  $width: 100px;  height: $width/2;  border: 1px+1px solid yellow;  margin-left: $width * 2;  padding-right: $width - 10px;}

编译后.wrapper {  height: 50px;  border: 2px solid yellow;  margin-left: 200ps;  padding-right: 90px;}

//颜色值计算$color: rgba(255, 0, 0, 0.5).p1 {  color: #010203 + #030201;}.p2 {  color: #010203 * 2;}.p3 {  color: opacity($color, 0.3);  background: transparentize($color, 0.25)}.p4 {  color: rgba(0, 255, 0, 0.75) + rgba(255, 0, 0, 0.75);}

编译后.p1 {  color: #040404;}.p2 {  color: #020406;}.p3 {  color: rgba(255, 0, 0, 0.8);  background: rgba(255, 0, 0, 0.25);}.p4 {  color: rgba(255, 255, 0, 0.75)}
 

原文地址:https://www.cnblogs.com/lidaylin/p/8569302.html

时间: 2024-08-30 02:35:33

sass总结的相关文章

gulp教程(sass,livereload,md5,css压缩,js压缩,img的base64)

环境 node -v  v6.10.3 npm -v  3.10.10 package.json如下: { "name": "zhcsdata", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": &

LSEE和SASS的区别

/Less中的注释,但这种不会被编译/* * 这也是Less中的注释,但是会被编译 *  * [Less中的变量] *  * 1.声明变量:@变量名:变量值: *   使用变量:@变量名 *  *  >>>Less中变量的类型: *  ①数字类         ①10px   ②字符串:无引号字符串 red        有引号字符串   "haha" *  ②颜色类red  #000000   rgb()    ③值列表类型:用逗号或空格分割     10px so

sass语法基础知识

文件后缀名 sass有两种后缀名文件:一种后缀名为sass,不使用大括号和分号:另一种就是我们这里使用的scss文件,这种和我们平时写的css文件格式差不多,使用大括号和分号.而本教程中所说的所有sass文件都指后缀名为scss的文件.在此也建议使用后缀名为scss的文件,以避免sass后缀名的严格格式要求报错. //文件后缀名为sass的语法 body background: #eee font-size:12px p background: #0982c1 //文件后缀名为scss的语法 b

sass学习笔记(五):sass的运算

(一).加法 加法运算是 Sass 中运算中的一种,在变量或属性中都可以做加法运算.如: .box {   width: 20px + 8in; } 编译出来的 CSS: .box {   width: 788px; } 但对于携带不同类型的单位时,在 Sass 中计算会报错,如下例所示: .box {   width: 20px + 1em; } 编译的时候,编译器会报错:"Incompatible units: 'em' and 'px'." (二).减法 Sass 的减法运算和加

10天精通Sass 之 处理字符串与数字的函数

Sass的函数简介 Sass中自备了一系列的功能函数,包括: - 字符串函数 - 数字函数 - 列表函数 - 颜色函数 - Introspection函数 - 三元函数 除了Sass中已提供的函数,我们还可以根据自己的需求定义函数,称为自定义函数. 字符串函数 * unquote($string) * :删除字符串中的引号 * quote($string) * :给字符串加引号 unquote()函数 用来删除字符串的引号,如果这个字符串没有引号,则返回原始字符串. .test1 { conte

SASS详解之沿袭(extend)

SASS详解之继承(extend) 每一个类名都有可能有另一个类名的所有样式和它自己的特定样式的.当一个div的身上有两个类名,一个是“one”,另一个是“two”的时候.如下 HTML代码 <div class="one two"> 梦龙小站 </div> CSS代码 .one {width:100px;height:100px;} .two {background:red;border:5px solid #000;} 这就意味着,我们要配备一个很好的记忆力

Sass 基本函数

Sass 中的常用函数 一.字符串函数 1. unquote($string): 删除字符串前后的引号,删除一对引号,如果这个字符串没有带有引号,将返回原始的字符串. 示例: .text1 { content: unquote("'hello'");} 2. quote($string): 给字符串添加引号,而且字符串中间有单引号或者空格时,需要用单引号或双引号括起,否则编译的时候将会报错(解决方案就是去掉空格,或者加上引号).同时 quote() 碰到特殊符号,比如: !.?.>

Sass 和 SCSS 有什么区别?

Sass 和 SCSS 其实是同一种东西,我们平时都称之为 Sass,两者之间不同之处有以下两点: 文件扩展名不同,Sass 是以“.sass”后缀为扩展名,而 SCSS 是以“.scss”后缀为扩展名 语法书写方式不同,Sass 是以严格的缩进式语法规则来书写,不带大括号({})和分号(;),而 SCSS 的语法书写和我们的 CSS 语法书写方式非常类似. Sass 语法 $font-stack: Helvetica, sans-serif //定义变量 $primary-color: #33

LESS 与 SASS

作为 CSS 的一种扩展,Less 不仅完全兼容 CSS 语法,而且连新增的特性也是使用 CSS 语法.这样的设计使得学习 Less 很轻松,而且你可以在任何时候回退到 CSS. 用less来控制样式,更加的方便,写好的less代码,可以通过编译生成css,这是对css非常强大的扩展,让我们写起代码时,更加的顺畅. 变量,在less中声明变量用@,例如: @test: red; .test{ color: @test; } 编译后: .test{ color: red; } 注:变量只能定义一次

SASS环境搭建及HBuilder中sass预编译配置

---------------------------------Ruby安装-------------------------------- 1.先下载一个ruby的安装文件:文件名可以搜索:rubyinstaller-2.3.1-x64 如图: 2.建议装到c盘(这里记住你的安装地址,后期有用) 3.勾选中间的path 然后就一路"南下",直到看到finish 4.win7系统的在左下角输入start,定位到Start Command Prompt with Ruby面板并调出 5