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预处理器 Less Sass,Scss 编译 Sourcemap调试
sass.less和stylus的安装使用和入门实践 SASS用法指南 Sass Basics CSS预处理器 css preprocessor 预处理器即preprocessor,预处 ...
- 比较三个 CSS 预处理器:Sass、LESS 和 Stylus(上)
前沿 : 第一次写不够成熟,可能描述有所错误,还请大家修改指正,我会对已完成的文章进行修改. 一.什么是CSS预处理器 CSS预处理器定义了一种新的语言,基本的思想是用一种专门的编程语言,开发者只需要 ...
- 初识css预处理器:Sass、LESS
这篇文章是初步介绍css预处理的,详细学习请移步官网~ 什么是css预处理器 CSS 预处理器是一种语言, 通俗易懂的话来说就是“用一种专门的编程语言,进行 Web 页面样式编写,再通过编译器转化为正 ...
- 【css预处理器】------css预处理器及sass基本介绍------【巷子】
001.什么是css预处理器? css预处理器定义了一种新的语言.用一种专门的编程语言,为css增加了一些编程的特性,将css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作.(通俗点说“” ...
- css预处理器(sass)
学过CSS的人都知道,它不是一种编程语言.你可以用它开发网页样式,但是没法用它编程.也就是说,CSS基本上是设计师的工具,不是程序员的工具.在程序员眼里,CSS是一件很麻烦的东西.它没有变量,也没有条 ...
- 比较三个 CSS 预处理器:Sass、LESS 和 Stylus(下)
五.Mixins (混入) Mixins 有点像是函数或者是宏,当你某段 CSS 经常需要在多个元素中使用时,你可以为这些共用的 CSS 定义一个 Mixin,然后你只需要在需要引用这些 CSS 地方 ...
- 【css预处理器】------sass的基本语法------【巷子】
001.安装sass 1.删除gem源:gem sources --remove https://rubygems.org/ 2.添加国内源:gem sources -a http://gems.ru ...
- css预处理器:Sass LASS Stylus
语法 Sass h1 { color: #0982C1; } h1 color: #0982c1 LESS h1 { color: #0982C1; } Stylus /* style.styl */ ...
- CSS预处理器——Sass、LESS和Stylus实践
CSS(Cascading Style Sheet)被译为级联样式表,做为一名前端从业人员来说,这个专业名词并不陌生,在行业中通常称之为“风格样式表(Style Sheet)”,它主要是用来进行网页风 ...
- 20190421-那些年使用过的CSS预处理器(CSS Preprocessor)
写在前面的乱七八糟的前言: emmm,不得不说,早上七点是个好时间,公园里跳广场舞的大妈,街边卖菜刀看报的大爷,又不得不说,广州图书馆是个好地方,该有的安静,该有的人气,听着楼下小孩子的声音,看着周围 ...
随机推荐
- Redis高可用之主从复制原理演进分析
Redis高可用之主从复制原理演进分析 在很久之前写过一篇 Redis 主从复制原理的简略分析,基本是一个笔记类文章. 一.什么是主从复制 1.1 什么是主从复制 主从复制,从名字可以看出,至少需要 ...
- Linux基础_7_文本显示
注:实质是针对标准输出文本的各种骚操作! 简单查看 注:初略加工后进行显示. cat -n 文件名 #查看文件内容并显示行号 tac 文件名 #逆序查看 more 文件名 less 文件名 #按?搜索 ...
- 抛砖系列之redis监控命令
前言 redis是一款非常流行的kv数据库,以高性能著称,其高吞吐.低延迟等特性让广大开发者趋之若鹜,每每看到别人发出的redis故障报告都让我产生一种居安思危,以史为鉴的危机感,恰逢今年十一西安烟雨 ...
- 21.drf视图系统组成及继承关系
APIView REST framework提供了一个 APIView 类,它是Django的 View 类的子类. APIView 类和Django原生的类视图的 View 类有以下不同: 传入的请 ...
- 云原生之旅 - 6)不能错过的一款 Kubernetes 应用编排管理神器 Kustomize
前言 相信经过前一篇文章的学习,大家已经对Helm有所了解,本篇文章介绍另一款工具 Kustomize,为什么Helm如此流行,还会出现 Kustomize?而且 Kustomize 自 kubect ...
- 基于JESD204B和PCIe DMA的多通道数据采集和回放系统
基于JESD204B和PCIe DMA的多通道数据采集和回放系统 在主机端PCIe驱动的控制和调度下,数据采集与回放系统可以同时完成对多个JESD204B接口AD数据的采集以及JESD204B接口DA ...
- 测试开发jmeter forEach控制器
测试开发jmeter forEach控制器 forEach控制器的使用场景:主要是对大量数据轮询就行接口请求 forEach控制器的使用前提:将数据进行参数化 测试开发jmeter forEach控制 ...
- iphoneApp Fidder设置
使用iphone 打开fidder 按照如上配置 安装完毕 然后访问计算机地址- 比如我的计算机ip地址是 192.168.2.10那么我需要在我的safari浏览器中输入192.168.2.10:8 ...
- 说说switch关键字
Switch语法 switch作为Java内置关键字,却在项目中真正使用的比较少.关于switch,还是有那么一些奥秘的. 要什么switch,我有if-else 确实,项目中使用switch比较少的 ...
- 6个tips缓解第三方访问风险
随着开发和交付的压力越来越大,许多企业选择依赖第三方来帮助运营和发展业务.值得重视的是,第三方软件及服务供应商和合作伙伴也是云环境攻击面的重要组成部分.尽管企业无法完全切断与第三方的关联,但可以在向他 ...