Scss的基本语法及使用

前言

SCSS 是 Sass 3 引入新的语法,其语法完全兼容 CSS3,并且继承了 Sass 的强大功能。也就是说,任何标准的 CSS3 样式表都是具有相同语义的有效的 SCSS 文件。

1.注释
2.嵌套规则
3. 变量: $
4.运算
5.合并文件:@import
6.继承:@extend
7.混入指令
8.判断:@if 和循环:@for
9.自定义函数

注释

在scss中有单行注释和双行注释,多行注释会被保留在输出的CSS中,而单行注释会被删除。

在scss中

/* This comment is
 * several lines long.
 * since it uses the CSS comment syntax,
 * it will appear in the CSS output. */

//abc

/*!This comment is important*/
.text {
    background-color: #fff;
}

在css中

/* This comment is
 * several lines long.
 * since it uses the CSS comment syntax,
 * it will appear in the CSS output. */
/*!This comment is important*/
.text {
  background-color: #fff;
}

嵌套规则

scss 允许将一个 CSS 样式嵌套进另一个样式中,内层样式仅适用于外层样式的选择器范围内

1.基本操作

在scss中:

#main{
  color: #00ff00;
  width: 97%;

  .redbox {
    background-color: #ff0000;
    color: #000000;
  }
}

在css中:

#main{
  color: #00ff00;
  width: 97%; 
}
#main .redbox {
   background-color: #ff0000;
   color: #000000; 
  }

2.引用父选择器:&

在scss中

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

在css中

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

3.嵌套属性

  CSS中有一些属性遵循相同的“命名空间”;比如,font-family, font-size, 和 font-weight都在 font 或 background 命名空间中。在CSS中,如果你想在同一个命名空间中设置一串属性,你必须每次都输出来。Scss为此提供了一个快捷方式:只需要输入一次命名空间,然后在其内部嵌套子属性
在scss中

.text {
  font: {
    family: fantasy;
    size: 30em;
    weight: bold;
  }
}

在css中

.text{
  font-family: fantasy;
  font-size: 30em;
  font-weight: bold; 
  }

变量: $

1.基本操作

在scss中

$width: 5em;

在css中

#main {
  width: $width;
}

2.在后面加 !global 标志

在scss中

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

在css中

#main {
  width: 5em;
}
#sidebar {
  width: 5em;
}

运算

在scss中
.text {
    $a: 50px;
    margin: (14px/3);  //margin: (14/3)+px;
    padding: round($a/3);
    top: 50px + 100px;
    left: $a * 2;
}

在css中

.text {
  margin: 4.66667px;
  padding: 17px;
  top: 150px;
  left: 100px;
}
scss中的数学函数
  • percentage(value):将一个不带单位的数转换成百分比值;
  • round(value):将数值四舍五入,转换成一个最接近的整数;
  • ceil(value):将大于自己的小数转换成下一位整数;
  • floor(value):将一个数去除他的小数部分;
  • abs(value):返回一个数的绝对值;
  • min(numbers…):找出几个数值之间的最小值;
  • max(numbers…):找出几个数值之间的最大值;
  • random(): 获取随机数

合并文件 @import

@import "./reset.scss";
@import "./common.scss";

继承 @extend

在scss中
.wh {
    width: 100px;
    height: 100px;
}
.bgcRed {
    background-color: red;
}
#main {
    @extend .wh;
    @extend .bgcRed;
    border: 1px solid #ccc;
}

在css中

.wh, #main {
  width: 100px;
  height: 100px;
}
.bgcRed, #main {
  background-color: red;
}
#main {
  border: 1px solid #ccc;
}

混入指令

### 1.定义一个混入@mixin 在scss中
@mixin fontstyle {
    font: {
        family: '楷体';
        size: 10px;
        weight: 600;
    }
    color:red;
}

2.引用混合样式:@include

在scss中

.text {
    @include fontstyle;
    margin: 10px;
}

在css中

.text {
  font-family: '楷体';
  font-size: 10px;
  font-weight: 600;
  color: red;
  margin: 10px;
}

3.mixin的强大之处,在于可以指定参数和缺省值

@mixin left($value: 10px){
    float: left;
    margin-right: $value;    
}
div{
    @include left(20px);
}

判断和循环

在scss中
li {
    @for $i from 1 to 6 {
        &:nth-child(#{$i}) {
            .img {
                background-image: url("../img/shop#{$i}.jpg");
            }
            @if $i>3 {
                background-color: #fff;
            }
        }
    }
}

在css中

li:nth-child(1) .img {
  background-image: url("../img/shop1.jpg");
}
li:nth-child(2) .img {
  background-image: url("../img/shop2.jpg");
}
li:nth-child(3) .img {
  background-image: url("../img/shop3.jpg");
}
li:nth-child(4) {
  background-color: #fff;
}
li:nth-child(4) .img {
  background-image: url("../img/shop4.jpg");
}
li:nth-child(5) {
  background-color: #fff;
}
li:nth-child(5) .img {
  background-image: url("../img/shop5.jpg");
}

自定义函数

在scss中
@function double($n) {
    @return $n * 2;
}
.text {
    width: double(5px);
}

在css中

.text {
  width: 10px;
}
### SCSS 语法简介 SCSS (Sassy CSS) 是一种扩展了标准 CSS 功能的预处理器语言,它提供了许多强大的特性来简化样式的编写过程。以下是关于 SCSS 的一些核心概念及其用法: #### 1. 基础语法 SCSS语法与传统 CSS 非常相似,但它支持更多高级功能,比如变量、嵌套规则以及混合宏等[^1]。 ```scss // 定义变量并应用到样式中 $primary-color: #333; body { color: $primary-color; } ``` 上述代码展示了如何定义一个 `$primary-color` 变量,并将其应用于 `body` 元素的颜色属性中。 --- #### 2. 嵌套规则 通过嵌套的方式,可以更直观地表示 HTML 结构中的父子关系,从而减少冗余代码[^3]。 ```scss nav { ul { margin: 0; padding: 0; list-style: none; } li { display: inline-block; } a { text-decoration: none; &:hover { color: red; } } } ``` 在这个例子中,所有的子元素都属于 `nav` 下面的一部分,这样不仅提高了可读性,还减少了重复书写父级选择器的工作量。 --- #### 3. 使用变量 变量可以在整个项目范围内共享某些固定的值,例如颜色或字体大小,方便统一管理和调整。 ```scss $font-stack: Helvetica, sans-serif; $primary-color: #333; body { font-family: $font-stack; color: $primary-color; } ``` 这里设置了两个全局变量——字体栈 (`$font-stack`) 和主要文字颜色 (`$primary-color`) 并在后续多次引用它们。 --- #### 4. 导入文件 (@import) 为了保持模块化开发原则,可以通过 `@import` 将多个独立的小型 SCSS 文件组合成最终的大规模样式表。 ```scss @import 'reset'; @import 'variables'; @import 'mixins'; body { background-color: $background-color; } ``` 注意,在现代前端构建工具链下(如 Webpack),推荐使用其他方式替代传统的 `@import` 方法以优化性能。 --- #### 5. 继承样式 (@extend) 当不同类之间存在公共部分时,可以利用 `@extend` 来实现样式的复用而不是复制粘贴相同的声明列表。 ```scss .message { border: 1px solid black; padding: 10px; } .success { @extend .message; border-color: green; } .error { @extend .message; border-color: red; } ``` 这种方式有助于降低维护成本,因为一旦 `.message` 类发生变化,则所有依赖它的派生类也会自动更新其表现形式。 --- #### 6. 混合宏 (@mixin / @include) 如果有一组特定的 CSS 属性需要频繁设置给不同的对象,则应该考虑创建自定义 mixin 函数以便重用逻辑结构。 ```scss @mixin flex-center($direction: row) { display: flex; justify-content: center; align-items: center; flex-direction: $direction; } .container { @include flex-center(column); } ``` 此片段演示了一个简单的布局辅助方法 —— 让容器内的内容居中显示的同时还可以指定排列方向,默认水平分布(`row`) 或垂直分布(`column`) . --- #### 7. 运算操作 除了基本的数据存储外,SCSS 支持直接执行数值计算任务,这使得动态生成复杂视觉效果变得更加容易[^2]。 ```scss $base-size: 1rem; .button { font-size: $base-size * 1.5; line-height: ($base-size + 0.5em); } ``` 以上实例说明了怎样基于已知尺寸单位推导新的比例关系出来用于实际渲染用途当中去。 --- #### 总结 综上所述,掌握了这些基础知识之后就可以着手实践自己的第一个 SCSS 工程啦!记住始终遵循 DRY(Don't Repeat Yourself) 设计理念尽可能消除不必要的重复劳动环节提升工作效率。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值