当前位置: 首页 > news >正文

scss预处理器对比css的优点以及基本的使用

本文主要在vue中演示,scss的基本使用。安装命令

npm install sass sass-loader --save-dev

变量

SCSS 支持变量,可将常用的值(如颜色、字体大小、间距等)定义为变量,方便重复使用和统一修改。

<template>
  <div>
    <div class="box">
       11111
    </div>

  </div>
</template>

<script>
export default {

}
</script>

<style lang="scss" scoped>
// 变量
$title-color:blue;
$text-font:30px;
.box{
    width: 200px;
    height: 200px; 
    //使用$title-color变量给边框设置颜色
    border: 1px solid $title-color; 
    //使用$text-font变量设置文字大小
    font-size: $text-font;
}
  
</style>>

嵌套规则 

scss支持嵌套规则,代码结构更符合html的嵌套逻辑

<template>
  <div>
    <div class="box2">
        <div class="title">this is title</div>
    </div>
    <div class="title">this is title2</div>
  </div>
</template>

<script>
export default {

}
</script>

<style lang="scss" scoped>
// 变量
$title-color:blue;
$text-font:30px;

// 嵌套规则
.box2{
    width: 200px;
    height: 200px;
    border: 1px solid $title-color;
    font-size: $text-font;
    .title{
        color: $title-color;
    }
//这里嵌套规则相当于css的 .box2 .title
}

  .title{
        color: red;
    }
</style>>

计算 

<template>
  <div>
    <div class="box3">这是一段文字aaaaaaaaaaa</div>
  </div>
</template>

<script>
export default {

}
</script>

<style lang="scss" scoped>
// 变量
$title-color:blue;
$text-font:30px;
$pad-value:10px;

.box3{
    // fit-content 元素宽度由内容撑开,在实际开发中注意浏览器兼容性
    width: fit-content;
    border: 1px solid $title-color;
    // scss支持计算 这里使用了变量$pad-value,
    // 效果等同于 padding: (10px * 2);
    padding: ($pad-value * 2);
}


</style>>

继承 

<template>
  <div>
     <div class="box4">继承样式</div>
  </div>
</template>

<script>
export default {

}
</script>

<style lang="scss" scoped>
// 变量
$title-color:blue;
$text-font:30px;
$pad-value:10px;
$normal-width:200px;
$noraml-height:100px;
.box4{
    width: $normal-width;
    height: $noraml-height;
     //继承 extendStyle类目的样式,extendStyle中的样式都会应用到box4类目上
    @extend .extendStyle;
}
.extendStyle{
    color: rgb(24, 211, 33); //该值即为页面上文字的绿色
    border: 1px solid $title-color;
    font-size: 50px;
    padding: 20px;
    font-weight: 700;
}


</style>>

 混合器

混合器(Mixins)是 SCSS 中一种非常强大的特性,它允许定义一组样式,然后在多个地方重复使用这些样式。可以把它想象成一个函数,这个函数封装了一系列的 CSS 规则,并且可以接受参数,这样在调用时可以根据不同的需求传入不同的值,从而实现样式的灵活复用。

<template>
  <div>
     <div class="box5">混合器</div>
  </div>
</template>

<script>
export default {

}
</script>

<style lang="scss" scoped>
// 变量
$title-color:blue;
$text-font:30px;
$pad-value:10px;
$normal-width:200px;
$noraml-height:100px;
// 无参数混合器
@mixin radiu-color{
    border: 2px solid red;
    border-radius: 10px;
}
// 有参数混合器
@mixin set-bgc($col){
    background-color: $col;
}
.box5{
    // 设置边框和圆角
    @include radiu-color();
    // 设置背景颜色为紫色
    @include set-bgc(purple);
    padding: 20px;
    color: orchid ;
    width: 100px;
}

</style>>

导入

在大型项目中,CSS 代码可能会变得非常庞大和复杂,为了更好地组织和管理这些代码,SCSS 提供了 `@import` 指令,允许你将多个 SCSS 文件导入到一个主文件中。这样可以将不同功能的样式代码分离到不同的文件中,提高代码的可维护性和可读性。

这里创建四个scss文件,_button.scss,_mixin.scss,_variable.scss,main.scss

//_variable.scss 用来存放变量
$title-color:blue;
$text-font:30px;
$pad-value:10px;
$normal-width:200px;
$noraml-height:100px;
$primary-color:rgb(243, 167, 25);
//_mixin.scss 用来存放混合器
// 无参数混合器
@mixin radiu-color{
    border: 2px solid red;
    border-radius: 10px;
}
// 有参数混合器
@mixin set-bgc($col){
    background-color: $col;
}
//_button.scss  封装按钮样式
@import '_variable';

// 使用变量和混合器
.button {
    @include radiu-color;
    background-color: $primary-color;
    color: white;
    padding: 10px 20px;
    border: none;
  }
//main.scss 中导入
// 导入变量文件
@import '_variable';
// 导入混合器文件
@import '_mixin';
// 导入按钮样式文件
@import '_button';

在组件中使用

<template>
  <div class="box">
     import导入
     <!-- 这里的类名button 就是 _button.scss中封装的样式 -->
     <button class="button">按钮</button>
  </div>
</template>

<script>
export default {

}
</script>

<style lang="scss" scoped>
// 导入
@import '@/css/main.scss';
 .box{
    // 验证是否生效
    width:$normal-width;
    color: $title-color;
 }
</style>

end 

如有误欢迎指正

相关文章:

  • 瑞盟MS35774/MS35774A低噪声 256 细分微步进电机驱动
  • 【动态规划】最长公共子序列问题 C++
  • 深入理解与使用 HashedWheelTimer:高效的时间轮定时器
  • python argparse 参数使用详解记录
  • Qt 制作验证码
  • SQL语句---特殊查询
  • 递归,搜索,回溯算法(一)
  • 多版本PHP开发环境配置教程:WAMPServer下MySQL/Apache/MariaDB版本安装与切换
  • ubuntu下docker 安装 graylog 6.1
  • HTML输出流
  • WebMvcConfigurer 的 addResourceLocations
  • Eplan许可管理的自动化工具
  • [Vue2]指令修饰符(一)
  • [问题收集]mysql主从分离过程中,数据不同步可能导致的后果以及应对策略
  • NFC 智能门锁全栈解决方案:移动端、服务器、Web 管理平台
  • src案例分享-逻辑漏洞
  • 软路由用联想j3710主板踩坑
  • 从0到1,解锁Ant Design X的无限可能
  • 能源革命新突破:虚拟电厂赋能微电网智能调控,构建低碳生态新格局
  • MCP插件使用(browser-tools-mcp为例)
  • “子宫内膜异位症”相关论文男性患者样本超六成?福建省人民医院发布情况说明
  • 王耀庆化身“罗朱”说书人,一人挑战15个角色
  • 印媒证实:至少3架印军战机7日在印控克什米尔地区坠毁
  • 李云泽:对受关税影响较大、经营暂时困难的市场主体,一企一策提供精准服务
  • 经济日报:落实落细更加积极的财政政策
  • 长沙天心阁举办古琴音乐会:文旅向深,让游客听见城市的底蕴