为何要使用PostCss
众所周知转换 px 单位的插件有不少,知名的有 postcss-px-to-viewport 和 postcss-pxtorem,前者是将 px 转成 vw,后者是将 px 转成 rem,精简了不经常使用的配置。将成为vw优先单位使用,以rem做为回退模式。考虑到vw在移动设备的支持度不如rem,这款插件很好的解决了该问题。而后简单的介绍下。javascript
安装指令
$ npm install @ moohng / postcss-px2vw --save-dev
复制代码
使用方法
首先建立一个.postcssrc.js文件,而后配置css
module.exports = { plugins: { '@moohng/postcss-px2vw': {} } } 复制代码
例子: 使用前:html
.box { border: 1px solid black; margin-bottom: 1px; font-size: 20px; line-height: 30px; } 复制代码
使用后:java
.box{ border: 1px solid black; margin-bottom: 1px; font-size: 0.625rem; font-size: 6.25vw; line-height: 0.9375rem; line-height: 9.375vw; } 复制代码
配置方面
viewportWidth:对应设计图的宽度,用于计算 vw。默认 750,指定 0 或 false 时禁用 rootValue:根字体大小,用于计算 rem。默认 75,指定 0 或 false 时禁用 unitPrecision:计算结果的精度,默认 5 minPixelValue:小于等于该值的 px 单位不做处理,默认 1 注意:该插件只会转换 px 单位。rootValue 通常建议设置成 viewportWidth / 10 的大小,将设计图分红10等分。因为浏览器有最小字体限制,若是设置得太小,页面可能跟预期不一致git
若是要使用 rem 单位,须要本身经过 js 来动态计算根字体的大小。若是将设计图分红 10 等分计算,那么根字体的大小应该是 window.innerWidth / 10。github
这样一个postCss的插件就配置完成了,但愿个人总结能给予你的帮助npm
sass:经常使用备忘
经常使用的sass笔记,快速的开发浏览器
1、变量sass
全部变量以$开头ide
$font_size: 12px;
.container{
font-size: $font_size;
}
复制代码
若是变量嵌套在字符串中,须要写在#{}中
$side : left; .rounded { border-#{$side}: 1px solid #000; } 复制代码
2、嵌套
层级嵌套
.container{ display: none; .header{ width: 100%; } } 复制代码
属性嵌套,注意,border后须要加上冒号:
.container { border: { width: 1px; } } 复制代码
能够经过&引用父元素,经常使用在各类伪类
.link{ &:hover{ color: green; } } 复制代码
3、mixin
简单理解,是能够重用的代码块,经过@include 命令
// mixin @mixin focus_style { outline: none; } div { @include focus_style; } 复制代码
编译后生成
div { outline: none; } 还可指定参数、缺省值 // 参数、缺省值 @mixin the_height($h: 200px) { height: $h; } .box_default { @include the_height; } .box_not_default{ @include the_height(100px); } 复制代码
编译后生成
.box_default { height: 200px; } .box_not_default { height: 100px; } 复制代码
4、继承
经过@extend,一个选择器能够继承另外一个选择器的样式。例子以下
// 继承 .class1{ float: left; } .class2{ @extend .class1; width: 200px; } 复制代码
编译后生成
.class1, .class2 { float: left; } .class2 { width: 200px; } 复制代码
5、运算
直接上例子
.container{ position: relative; height: (200px/2); width: 100px + 200px; left: 50px * 2; top: 50px - 10px; } 复制代码
编译后生成
.container { position: relative; height: 100px; width: 300px; left: 100px; top: 40px; } 复制代码
插入文件
用@import 来插入外部文件
@import "outer.scss"; 复制代码
也可插入普通css文件
@import "outer.css"; 复制代码
自定义函数
经过@function 来自定义函数
@function higher($h){ @return $h * 2; } .container{ height: higher(100px); } 复制代码
编译后输出
.container { height: 200px; } 复制代码
结语
创做不易,若是对你们有所帮助,但愿你们点赞支持,有什么问题也能够在评论区里讨论😄~