PostCss 从0开始
PostCss
摘自
http://ju.outofmemory.cn/entry/215105
http://www.w3cplus.com/PostCSS/postcss-deep-dive-preprocessing-with-precss.html
PostCss是啥
官方定义
“PostCSS is a tool for transforming CSS with JS plugins. These plugins can support variables and mixins, transpile future CSS syntax, inline images, and more.
PostCss是一个可以运行在Gulp Grunt Webpack中的插件
同时它又是一套CSS处理插件的一个环境
PostCss怎么用
基本和Sass一样 要和Sass那样使用变量
css
/PostCSS是一个Gulp插件 同时是一套Css插件的运行环境/
/PostCSS和Sass语法最为相似 PostCSS将Sass那样的表达形式直接写在css文件中/
.menu1 {
width: 100%;
a {
text-decoration: none;
}
&::before {
content: '';
}
}
/使用PreCSS插件(PostCSS中的插件)来做变量 条件处理/
/*PreCSS is a tool that allows you to use Sass-like markup in your CSS files.
Enjoy a familiar syntax with variables, mixins, conditionals, and other goodies.
*/
/LESS中使用@符声明变量,比如@color: #ccc;
Stylus中使用$符声明变量,比如$color=#ccc;
Sass中使用$符声明变量,比如$color: #ccc;/
$text_color: #232323;
$blue: #056ef0;
$column: 200px;
body {
color: $text_color;
}
.menu {
width: calc(4 * $column);
}
.menu_link {
background: $blue;
width: $column;
}
/条件/
/和Sass一样 判断表达式两边要有空格 否则不识别表达式/
$column_layout: 2;
.column {
@if $column_layout == 2 {
width: 50%;
float: left;
}
@else {
width: 100%;
}
}
/循环/
.for-test {
@for $i from 1 to 3 {
p:nth-of-type($i) {
margin-left: calc( 100% / $i);
}
}
}
/*each循环在循环体中不是 $icon 而是 $(icon) */
.each-test {
@each $icon in (foo, bar, baz) {
.icon-$(icon) {
background: url('icons/$(icon).png');
}
}
}
/包含mixin/
/@define-mixin 规则名 变量名/
@define-mixin icon $name {
padding-left: 16px;
&::after {
content: "";
background-url: url(/icons/$(name).png);
}
}
.search {
@mixin icon search;
}
.search2{
@mixin icon search2;
}
/扩展/
@define-extend bg-green {
background: green;
}
.notice--clear {
@extend bg-green;
}
.xx-clear{
@extend bg-green;
}
/扩展会减少冗余 得到的结果如下/
/.notice--clear, .xx-clear {
background: green;
}/
/值的复制/
/最后得到 margin: 20px;padding: 20px;/
.heading {
margin: 20px;
padding: @margin;
}
###结合Gulp
```javascript```
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('css', function() {
return gulp.src('src/*.css')
.pipe(sourcemaps.init())
.pipe(postcss([require('autoprefixer'), require('precss')]))
//.pipe(postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ]))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('build/'));
});
gulp.task('watch',function() {
gulp.watch('src/*.css', ['css']);
});
gulp.task('default', ['css','watch']);
结合Webpack
注意,webpack对css的各个资源引用都有检查,比如背景图片不存在的话,会有Error
javascript
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: path.join(__dirname, 'main.js'),
output: {
path: path.join(__dirname, 'out'),
publicPath: "./out/",
filename: 'bundle.js'
},
module: {
loaders: [{
test: /.css$/,
loader: "style-loader!css-loader!postcss-loader"
}]
},
postcss: function() {
return [require('autoprefixer'), require('precss')];
}
}
PS
main.js和style.css在同一个文件夹中
在main.js中需要引入这个css文件才行```var css = require('./style.css');```
PostCss 从0开始的更多相关文章
- PostCSS理解与运用
1.PostCSS是什么 它可以被理解为一个平台,可以让一些插件在上面跑 它提供了一个解析器,可以将CSS解析成抽象语法树 通过PostCSS这个平台,我们能够开发一些插件,来处理CSS.热门插件如a ...
- 从 0 到 1 到完美,写一个 js 库、node 库、前端组件库
之前讲了很多关于项目工程化.前端架构.前端构建等方面的技术,这次说说怎么写一个完美的第三方库. 1. 选择合适的规范来写代码 js 模块化的发展大致有这样一个过程 iife => commonj ...
- json-sever 配置与应用
先安装 node.js. 全局安装 json-server: npm install json-server -g 检查是否安装成功: json-server -h 安装好后,json-server的 ...
- 发布vue插件到npm上
总体分为2个步骤 一,先写好插件 二,发布到npm上面 一,写vue插件 vue有一个开放的方法install,在vue插件需要写在这个方法里面,在vue官网,里面说的很清楚,这个方法里面可以是全局方 ...
- npm包发布过程
在上一章节中,我封装了一个基于react的树状组件,后来想把它发布到npm上,下面主要介绍一下发布过程中遇到的问题: 1.去注册npm账号,注册地址(https://www.npmjs.com), 再 ...
- Angular CLI: 1.6.7 入门
当你使用npm或者yarn也安装不了angular-cli时,请使用淘宝镜像. Step1 npm i -g cnpm --registry=https://registry.npm.taobao.o ...
- 微信小程序~调试
小程序调试面板 模拟器 模拟器模拟微信小程序在客户端真实的逻辑表现,对于绝大部分的 API 均能够在模拟器上呈现出正确的状态. 自定义编译 点击工具栏中的编译按钮或者使用快捷键 Ctrl(⌘) + B ...
- ZAM 3D 制作简单的3D字幕 流程(二)
原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...
- ZAM 3D 制作3D动画字幕 用于Xaml导出
原地址-> http://www.cnblogs.com/yk250/p/5662788.html 介绍:对经常使用Blend做动画的人来说,ZAM 3D 也很好上手,专业制作3D素材的XAML ...
随机推荐
- 《C++ 标准库》读书笔记 - 第二章 Introduction to C++ and the Standard Library
1. History of the C++ Standards 1.1 History of the C++ Standards C++98 -> C++03 -> TR1 -> C ...
- mysql表结构设计
允许NULL值的字段,数据库在进行比较操作时,会先判断其是否为NULL,非NULL时才进行值的必对.因此基于效率的考虑,所有字段均不能为空,即全部NOT NULL: 对于变长表,由于记录大小不同,在其 ...
- android 快速创建一个新的线程
要给一个activity做成子线程的模式 第一种:直接创建子线程并启动 private Thread newThread; //声明一个子线程 new Thread() { @Override pub ...
- 获取当前url并指定url中的字符 效果
效果介绍:1.获取当前url 2.通过获取的url,找到指定的字符并判断 3.如果是指定字符,页面跳转到博客园:如果不是页面跳转到百度 例如:http://www.cnblogs.com/fs521c ...
- linux命令学习03-grep
实例1.查找某个进程 #ps -ef | grep ssh root 1771 1 0 12:07 ? 00:00:00 /usr/sbin/sshdroot 2362 1771 0 16:34 ? ...
- knockout 与checkbox联动
knockout 通过teplate实现简单的代码实现复杂的操作绑定checkbox,代码如下自我感觉很赞!!! 前台HTml <ul data-bind="template: { n ...
- 关于Program Size
Program Size: Code=86496 RO-data=9064 RW-data=1452 ZI-data=16116 Code是代码占用的空间,RO-data是 Read Only 只读常 ...
- 旧的VirtualBox News(从1.3.4开始)
https://linuxtoy.org/archives.html https://linuxtoy.org/archives/virtualbox-134.html http://www.cnbl ...
- Spring、Hello Spring
1.引入Spring jar包 2.新建一个Person 接口和Person Bean public interface PersonIService { public void helloSprin ...
- LinqToSQL实例参见
/// <summary> /// (增)向表中插入数据 /// </summary> public void InsertData() { LinqToSQLDataCont ...