CSS预处理器scss/sass语法及使用教程
发布时间:2023-01-05 16:42:30 作者:半糖也很甜吖
我要评论

scss在css基础语法上面增加了变量 (variables)、嵌套 (nested rules)、混合 (mixins)、导入 (inline imports) 等高级功能,使用scss可以很方便的提高开发效率,这篇文章主要介绍了css预处理器scss/sass语法以及使用,需要的朋友可以参考下
scss
scss在css基础语法上面增加了变量 (variables)、嵌套 (nested rules)、混合 (mixins)、导入 (inline imports) 等高级功能,使用scss可以很方便的提高开发效率
scss语法以.scss
文件后缀结尾,其中语法格式有两种sass
,scss
,两种语法在书写风格有差异,如下代码所示
scss
.container { width: 100px; height: 100%; .nav { width: 100px; } }
sass
.container width: 100px; height: 100%; .nav width: 100px;
语法
嵌套规则 (常用)
scss允许将一套css样式嵌入另一套样式中,外层的容器将作为内层容器的父选择器,如下代码
.container { width: 500px; height: 100px; header { width: 100%; height: 20%; } main { width: 100%; height: 20%; } footer { width: 100%; height: 20%; } }
编译后
.container { width: 500px; height: 100px; } .container header { width: 100%; height: 20%; } .container main { width: 100%; height: 20%; } .container footer { width: 100%; height: 20%; }
父选择器 (常用)
有时需要在内层样式内选择外层的父元素,那么就可以使用&
符号,如下代码所示
.container { width: 500px; height: 100px; &_header { width: 100%; height: 20%; &:hover { color: red($color: #000000); } } &_main { width: 100%; height: 20%; &:disabled { color: red; } } &_footer { width: 100%; height: 20%; &::after { position: absolute; content: ''; } } }
编译后
.container { width: 500px; height: 100px; } .container_header { width: 100%; height: 20%; } .container_header:hover { color: 0; } .container_main { width: 100%; height: 20%; } .container_main:disabled { color: red; } .container_footer { width: 100%; height: 20%; } .container_footer::after { position: absolute; content: ''; }
属性简写 (不常用)
.container { width: 500px; height: 100px; font: { family: fantasy; size: 30em; weight: bold; } background: { image: url('xxx'); size: 100%; } }
编译后
.container { width: 500px; height: 100px; font-family: fantasy; font-size: 30em; font-weight: bold; background-image: url('xxx'); background-size: 100%; }
变量 (常用)
scss中使用$
符号定义变量
- 全局变量
在scss文件顶部定义的变量,为全局变量
$font-color: red; $font-size: 18px; $font-size-base: $font-size; .text { color: $font-color; font-size: $font-size; } span { font-size: $font-size-base; }
编译后
.text { color: red; font-size: 18px; } span { font-size: 18px; }
- 局部变量
在属性内定义的变量为块级变量
.text { $font-color: red; $font-size: 18px; $font-size-base: $font-size; h1 { color: $font-color; font-size: $font-size; span { color: $font-color; font-size: $font-size; } } }
编译后
.text h1 { color: red; font-size: 18px; } .text h1 span { color: red; font-size: 18px; }
运算 (常用)
scss中支持+
-
*
/
运算
$base-width: 10; $small-width: 30; $large-width: $base-width + $small-width; .div { width: $large-width + px; } .div1 { width: $small-width - $base-width + px; } .div2 { width: $small-width * $base-width + px; } .div2 { width: calc($small-width / $base-width) + px; }
编译后
.div { width: 40px; } .div1 { width: 20px; } .div2 { width: 300px; } .div2 { width: 3px; }
@extend
scss允许使用@extend
集成其他样式规则
.item { width: 100%; height: 20%; background-color: red; } .item-1 { @extend .item; border: 1px solid blue; } .item-2 { @extend .item; border: 2px solid blue; }
编译后
.item, .item-2, .item-1 { width: 100%; height: 20%; background-color: red; } .item-1 { border: 1px solid blue; } .item-2 { border: 2px solid blue; }
@if
当条件满足时,输入对应的样式
p { @if 1 + 1 == 2 { border: 1px solid; } @if 5 < 3 { border: 2px dotted; } @if null { border: 3px double; } } $type: monster; p { @if $type == ocean { color: blue; } @else if $type == matador { color: red; } @else if $type == monster { color: green; } @else { color: black; } }
编译后
p { border: 1px solid; } p { color: green; }
@for
- 语法一:
@for $var from <start> through <end>
从start开始,包含end
@for $i from 1 through 3 { .item-#{$i} { width: 2em * $i; } }
编译后
.item-1 { width: 2em; } .item-2 { width: 4em; } .item-3 { width: 6em; }
- 语法二:
@for $var from <start> to <end>
从start开始,不包含end
@for $i from 1 to 3 { .item-#{$i} { width: 2em * $i; } }
编译后
.item-1 { width: 2em; } .item-2 { width: 4em; }
到此这篇关于CSS预处理器scss/sass语法及使用教程的文章就介绍到这了,更多相关css预处理器scss内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
相关文章
- scss在css基础语法上面增加了变量 (variables)、嵌套 (nested rules)、混合 (mixins)、导入 (inline imports) 等高级功能,使用scss可以很方便的提高开发效率,这篇文章主2023-01-05
- 本文主要介绍了利用css动画实现节流,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-01-04
- 2022卡塔尔世界杯正在如火如荼的进行之中,作为“诸神的黄昏”,本届世界杯备受瞩目,足坛巅峰老将c罗,梅西,内马尔也将随本次世界杯退役,一代人的青春到此结束!本篇我2023-01-04
- 在页面中,我们常用id、class以及内联样式表来设置我们的组件CSS,在网页中css样式根据css优先级来使用,优先级高的会覆盖优先级低的css样式,本文通过实例代码给大家详细2023-01-03
- 本文主要介绍了css实现文字充电效果的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习2023-01-03
- 这篇文章主要介绍了table不让td文字溢出操作方法,需要的朋友可以参考下2022-12-16
- 这篇文章主要介绍了table设置超出部分隐藏,鼠标移上去显示全部内容的实现方法,需要的朋友可以参考下2022-12-16
- 这篇文章主要介绍了CSS使用SVG实现动态分布的圆环发散路径动画,第一时间看到这个需求想到的就是 SVG 或者 Canvas,但是由于开发时可能还需要插入其他元素,所以这里还是希2022-10-27
- 这篇文章主要介绍了CSS中理解层叠性及权重如何分配,CSS的三大特性,分别是层叠性、优先性和继承性,本文给大家详细讲解,对css层叠性权重相关知识感兴趣的朋友跟随小编一起2022-10-24
- 这篇文章主要介绍了CSS 鼠标点击拖拽效果,我们还是仅仅通过 CSS,来实现一种丝滑的鼠标点击拖动元素移动的效果,本文通过实例代码效果展示给大家介绍的非常详细,对大家的2022-10-10
最新评论