马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
使用流程
演变汗青
.Sass
从前的 sass 的文件名后缀是 .sass, 下面为其语法- h1
- color: #000
- background: #fff
复制代码 .Scss
在 3.x 以后的版本, 引入了 .scss 的语法, 雷同于我们的 css 语法
有了 "花括号“、“分号”, 可以直接兼容 老的项目的 css- h1 {
- color: #000;
- background: #fff;
- }
复制代码 留意: 一个项目中可以混淆使用两种语法, 但不能在一个文件中使用两种语法
安装方法
- // 直接将输出结果输出在终端
- sass test.scss
复制代码- // 将结果以文件输出, 后面再加 .css 的文件名即可
- sass test.scss test.css
复制代码
根本使用
变量的界说与使用
sass 变量界说, 使用 $ 符号- /* 定义变量与值 */
- $bgcolor: lightblue;
- $textcolor: darkblue;
- $fontsize: 18px;
- /* 使用变量 */
- body {
- background-color: $bgcolor;
- color: $textcolor;
- font-size: $fontsize;
- }
复制代码 扩展: css 变量的界说与使用- /* 变量的定义 */
- /* -- 两个减号 */
- :root {
- --main-bg-color: brown;
- }
- /* 变量的使用 */
- .element {
- background-color: var(--main-bg-color);
- }
复制代码 文件的引用
直接使用 @import, 即可导入, 这个语法原生 css 也是有的- // 单文件引入
- @import "variables";
- // 多文件引入
- @import "variables", "compass/reset";
复制代码 如果要使用 css 原生 @import , 可以按以下既定规则
- 当 @import 后边跟着文件名是以 .css 末了的时间
- 当 @import 后边跟着的是 http:// 开头的字符串的时间
- 当 @import 后边跟着的是 url() 开头的字符串的时间
- 当 @import 后边跟着的是 media queries 开头的字符串的时间
如果要使用 sass 的 @import , 可以按以下既定规则
- 没有文件后缀名的时间, sass 会添加 .scss 大概 .sass 的后缀
- 同一个目次下, 全部文件不能重名
扩展: 如果你不想 sass 文件被编译, 可以在前面加 _ 下划线, 好比 _filename 如许文件就会被忽略
表明的差异
sass 有两种表明方式- // test.scs 文件
- // 主题样式
- .headline {
- font-family: $headline-ff;
- }
- /* 页面样式 */
- .main-sec {
- font-family: $main-sec-ff;
- }
复制代码 那这两种样式有何差异呢? 我们看下编译后的文件- /* test.css 文件, 由 test.scss 编译而来 */
- .headline {
- font-family: Braggadocio;
- }
- /* 页面样式 */
- .main-sec {
- font-family: Arial;
- }
复制代码 可以看到, .headline 没有表明信息, 而 .main-sec 有, 分析 // 双斜线表明, 不会被编译
扩展: css 表明, 只能使用 /* */ 举行表明
作用域
Sass 具有作用域雷同于 JavaScript 的中的函数作用域- $myColor: red;
- h1 {
- $myColor: green; // 只在 h1 里头有用,局部作用域
- color: $myColor;
- }
- p {
- color: $myColor;
- }
复制代码 编译效果- h1 {
- color: green;
- }
- p {
- color: red;
- }
复制代码 嵌套规则与属性
嵌套规则的使用- // scss 文件
- nav {
- ul {
- margin: 0;
- padding: 0;
- list-style: none;
- }
- li {
- display: inline-block;
- }
- a {
- display: block;
- padding: 6px 12px;
- text-decoration: none;
- }
- }
复制代码- /* 编译后 .css 文件 */
- nav ul {
- margin: 0;
- padding: 0;
- list-style: none;
- }
- nav li {
- display: inline-block;
- }
- nav a {
- display: block;
- padding: 6px 12px;
- text-decoration: none;
- }
复制代码 嵌套属性的使用- // scss 文件
- font: {
- family: Helvetica, sans-serif;
- size: 18px;
- weight: bold;
- }
- text: {
- align: center;
- transform: lowercase;
- overflow: hidden;
- }
复制代码- /* 编译后 .css 文件 */
- font-family: Helvetica, sans-serif;
- font-size: 18px;
- font-weight: bold;
- text-align: center;
- text-transform: lowercase;
- text-overflow: hidden;
复制代码 混入的使用
混入的根本使用
混入的界说 @mixin- @mixin mixin-name {
- color: red;
- font-size: 25px;
- font-weight: bold;
- border: 1px solid blue;
- }
复制代码 混入的使用 @include- selector {
- @include mixin-name;
- }
复制代码 混入可以通报变量- /* 混入接收两个参数 */
- @mixin bordered($color, $width) {
- border: $width solid $color;
- }
- .myArticle {
- @include bordered(blue, 1px); // 调用混入,并传递两个参数
- }
- .myNotes {
- @include bordered(red, 2px); // 调用混入,并传递两个参数
- }
复制代码 编译后的 .css 文件- .myArticle {
- border: 1px solid blue;
- }
- .myNotes {
- border: 2px solid red;
- }
复制代码 也可以设置默认值- @mixin sexy-border($color, $width: 1in) {
- border: {
- color: $color;
- width: $width;
- style: dashed;
- }
- }
- p { @include sexy-border(blue); }
- h1 { @include sexy-border(blue, 2in); }
复制代码 编译效果- p {
- border-color: blue;
- border-width: 1in;
- border-style: dashed;
- }
- h1 {
- border-color: blue;
- border-width: 2in;
- border-style: dashed;
- }
复制代码 不定参数的情况下, 我们可以使用 ... 来设置参数- @mixin box-shadow($shadows...) {
- -moz-box-shadow: $shadows;
- -webkit-box-shadow: $shadows;
- box-shadow: $shadows;
- }
- .shadows {
- @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
- }
复制代码 编译效果- .shadows {
- -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
- -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
- box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
- }
复制代码 扩展: 欣赏器前缀使用混入, 会很方便- @mixin transform($property) {
- -webkit-transform: $property;
- -ms-transform: $property;
- transform: $property;
- }
- .myBox {
- @include transform(rotate(20deg));
- }
复制代码 编译效果- .myBox {
- -webkit-transform: rotate(20deg);
- -ms-transform: rotate(20deg);
- transform: rotate(20deg);
- }
复制代码 继续
根本使用
场景: 三个样式只有少量区别, 此中一个样式, 别的两个都有, 那么就可以使用继续- .button-basic {
- border: none;
- padding: 15px 30px;
- text-align: center;
- font-size: 16px;
- cursor: pointer;
- }
- .button-report {
- @extend .button-basic;
- background-color: red;
- }
- .button-submit {
- @extend .button-basic;
- background-color: green;
- color: white;
- }
复制代码 编译效果- .button-basic, .button-report, .button-submit {
- border: none;
- padding: 15px 30px;
- text-align: center;
- font-size: 16px;
- cursor: pointer;
- }
- .button-report {
- background-color: red;
- }
- .button-submit {
- background-color: green;
- color: white;
- }
复制代码 继续多个选择器- .one {
- width:100px;height:100px;
- }
- .two {
- /*继承的样式*/
- @extend .one, .three;
- /*独立的样式*/
- background:red;
- border:5px solid #000;
- }
- .three {
- padding:10px;
- }
复制代码 编译效果- .one, .two {
- width: 100px;
- height: 100px;
- }
-
- .two {
- /*独立的样式*/
- background: red;
- border: 5px solid #000;
- }
-
- .three, .two {
- padding: 10px;
- }
复制代码 使用 %, 用来构建只用来继续的选择器
不使用 % 的情况- .error {
- color: #f00;
- }
- .serious-serror {
- @extend .error;
- border: 1px #f00;
- }
复制代码 编译效果- .error, .serious-serror {
- color: #f00;
- }
- .serious-serror {
- border: 1px #f00;
- }
复制代码 使用 % 的情况- %error {
- color: #f00;
- }
- .serious-serror {
- @extend %error;
- border: 1px #f00;
- }
复制代码 编译效果- .serious-serror {
- color: #f00;
- }
- .serious-serror {
- border: 1px #f00;
- }
复制代码 可以看到使用 % 的选择器 .error 就不体现了
扩展: extend 不可以继续嵌套选择器
选择器
在函数内部使用 & 体现当前 (& add 符合)- .webdome-sec {
- &:hover {
- background-color: #f5f5f5;
- }
- }
复制代码 编译效果- .webdome-sec:hover {
- background-color: #f5f5f5;
- }
复制代码 函数
@function 界说函数, 知道就好, 根本不消
拼接
- <template>
- <div class="box">
- <div class="box-content">
- <div class="box-content-header"></div>
- <div class="box-content-body"></div>
- </div>
- </div>
- </template>
- <style lang="scss">
- // 拼接方式 写样式代码,减少类名长度,且优雅
- .box {
- width: 200px;
- height: 200px;
- background-color: aqua;
- &-content {
- display: flex;
- // 继承整合相同样式
- &-size {
- width: 50px;
- height: 50px;
- }
- &-header {
- @extend .box-content-size;
- background-color: red;
- }
- &-body {
- @extend .box-content-size;
- background-color: yellow;
- }
- }
- }
- </style>
复制代码
|