scss一些用法

中文文档地址:https://www.sasscss.com/docs/#css-extensions

1. 嵌套样式

允许将一个 CSS 样式嵌套进另一个样式中。

body {
  min-height: 100vh;
  .box {
    height: 100px;
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    .content {
      height: 50px;
      width: 50px;
      background: #999;
    }
  }
}

2. 引用父级选择器: ‘&’

a {
  font-weight: bold;
  text-decoration: none;
  &:hover {
    text-decoration: underline;
  }
}

& 必须出现在的选择器的开头位置, 可以跟随后缀。

#main {
  color: black;
  &-sidebar { border: 1px solid; }
}

2. 嵌套属性

.box {
    font: {
      family: fantasy;
      size: 30em;
      weight: bold;
    }
}
// 编译为
.box {
  font-family: fantasy; 
  font-size: 30em;  
  font-weight: bold; 
}

3. 变量 ‘$’

$width: 100px;
.box {
  width: $width;
}

作用域: 不在任何嵌套选择器内定义的变量则在可任何地方使用,嵌套在选择器内的只有嵌套层级范围内可用。可以在后面添加 !global 标志,就可以全局引用(不推荐使用)

#main {
  $width: 5em !global;
      width: $width;
}

#sidebar {
  width: $width;
}

4. 运算

支持对数字标准的算术运算(加法 +,减法 - ,乘法 * ,除法 / 和取模 % ),数字支持关系运算符(<, >, <=, >=),并且所有类型支持相等运算符(==, !=)

  1. 除法 /
p {
  font: 10px/8px;             // 原生的CSS,不作为除法
  $width: 1000px;
      width: $width/2;            // 使用了变量, 作为除法
  width: round(1.5)/2;        // 使用了函数, 作为除法
  height: (500px/2);          // 使用了括号, 作为除法
  margin-left: 5px + 8px/2px; // 使用了 +, 作为除法
  font: (italic bold 10px/8px); // 在一个列表(list)中,括号可以被忽略。
}

如果你想纯CSS 的/和变量一起使用(愚人码头注:即/不作为除法使用),你可以使用#{}插入他们。例如:

p {
  $font-size: 12px;
      $line-height: 30px;
  font: #{$font-size}/#{$line-height};
}

5. mixin

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

可以设置参数

@mixin large-text($color) {
  font: {
    family: Arial;
    size: 20px;
    weight: bold;
  }
  color: $color;
}

.page-title {
  $green: green;
  @include large-text($green);
  padding: 4px;
  margin-top: 10px;
}

// 可以引入默认值,要是没有传参数,就使用默认值

@mixin large-text($color: red) {
  font: {
    family: Arial;
    size: 20px;
    weight: bold;
  }
  color: $color;
}

.page-title {
  @include large-text;
  padding: 4px;
  margin-top: 10px;
}
// color 会编译为 red

6. 函数

@function px($npx){
  @return $npx/375 * 100vw
}

.icons {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 px(20);
  margin-top: px(20);
  .icon {
    background: #eee;
    height: px(48);
    width: px(48);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-shrink: 0;
  }
}
// 可以适应不同尺寸手机端

7. 媒体查询

$break-small: 320px;
$break-large: 1024px;

@mixin respond-to($media) {
  @if $media == handhelds {
    @media only screen and (max-width: $break-small) { @content; }
  }
  @else if $media == medium-screens {
    @media only screen and (min-width: $break-small + 1) and (max-width: $break-large - 1) { @content; }
  }
  @else if $media == wide-screens {
    @media only screen and (min-width: $break-large) { @content; }
  }
}
.profile-pic {
  float: left;
  width: 250px;
  @include respond-to(handhelds) { width: 100% ;}
  @include respond-to(medium-screens) { width: 125px; }
  @include respond-to(wide-screens) { float: none; }
}

// 编译后

.profile-pic {
  float: left;
  width: 250px;
}
@media only screen and (max-width: 320px) {
  .profile-pic {
    width: 100%;
  }
}
@media only screen and (min-width: 321px) and (max-width: 1023px) {
  .profile-pic {
    width: 125px;
  }
}
@media only screen and (min-width: 1024px) {
  .profile-pic {
    float: none;
  }
}

8. 一个按钮小例子 (hover 之后上下,左右抖动)

*{box-sizing: border-box; padding: 0; margin: 0;}
body{
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
@mixin createButton($color){
  background: $color;
  box-shadow: 0 4px 0 darken($color,15%)
}
.buttonWrapper{
  button {
    margin: 0 40px;
    border: none;
    padding: 10px 20px;
    border-radius: 4px;
    color: #fff;
    font-size: 18px;
    &:first-child {
      $blue: #55acee;
      @include createButton($blue);
      &:hover {
        animation-duration: 0.3s;
        animation-name: left-right; 
      }
    }
    &:nth-child(2) {
      $green: #2ecc71;
      @include createButton($green);
        &:hover{
          animation-name: up-down;
          animation-duration: 0.3s;
      }
    }
  }
}
$n: 10%;
$step: 25%;
@keyframes left-right {
  @for $i from 0 to 4 {
    #{$i * $step}{
      @if $i % 2 == 0 {
        transform: translateX($n)
      }@else{
        transform: translateX(-$n)
      }
    }
  }
  100%{
    transform: translateX(0)
  }
}
$n2: 20%;
$step: 25%;
@keyframes up-down {
  @for $i from 0 to 4 {
    #{$i * $step}{
      @if $i % 2 == 0 {
        transform: translateY($n2)
      }@else{
        transform: translateY(-$n2)
      }
    }
  }
  100%{
    transform: translateY(0)
  }
}
预览
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,489评论 1 45
  • 慕课网学习笔记 什么是 CSS 预处理器? CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言...
    打铁大师阅读 1,293评论 0 1
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,746评论 0 10
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,209评论 0 3
  • 你很像一艘船抛了锚 勾在我的心上,而不是耳边的发梢 你颠簸在风雨和浪涛 不曾随波去远,却划开了深深地一道 . 那艘...
    水摇绢阅读 277评论 0 1