博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostCss简介
阅读量:4287 次
发布时间:2019-05-27

本文共 3686 字,大约阅读时间需要 12 分钟。

官网地址:

源代码地址:

来自官方的文档上写着的是是一个插件转换样式表的工具。这些插件能够检验你的、支持变量和混合,转化的新特性语法、行内图片等。

其中一个比较为人所知的插件AutoPrefixer就是出自这里,目前上面已经超过200多个插件在

解决全局CSS问题

  • to explicitly set plugins in CSS and execute them only for the current file.
  • or automatically isolates selectors in components.
  • uses local reset in component, instead of global one.
  • adds all: initial support to reset all inherit styles.
  • adds media queries for component size or parent background.

使用最新的CSS特性

  • adds vendor prefixes, using data from Can I Use.
  • allows you to use future CSS features today (includes autoprefixer).

更高的CSS可读性

  • contains plugins for Sass-like features like nesting or mixins.
  • sorts rules content with specified order.
  • adds and extends numerous shorthand properties.

图片和字体

  • inserts image dimensions and inlines files.
  • generates image sprites.
  • generates all the @font-face rules needed in CSS.
  • allows to inline SVG and customize its styles.
  • allows to write simple SVG directly in CSS.

Linters

  • is a modular linter for CSS.
  • lints CSS for browser support, using data from Can I Use.
  • helps maintain a consistent color palette.

Other

  • is feature-rich calc() grid system by Jeet author.
  • is a modular CSS minifier.
  • mirrors styles for right-to-left locales.

语法类

PostCSS can transform styles in any syntax, not just CSS.原文来自:http://caibaojian.com/postcss.html

  • to work with SCSS (but does not compile SCSS to CSS).
  • to write styles in JS or transform React Inline Styles, Radium or JSS.
  • finds and fixes CSS syntax errors.
  • converts a CSS string to highlighted .

相关文章

演示

Autoprefixer

PostCSS最有名的插件是。如名所示,可以自动为你添加浏览器私有前缀。它的添加值会参考及你设定的浏览器支持范围,因此相当可靠。下面是一个示例(以我设定的浏览器支持范围):

.container{    display: flex;}

编译后:

//code from http://caibaojian.com/postcss.html.container{    display: -webkit-box;    display: -webkit-flex;    display: -ms-flexbox;    display: flex;}

postcss-nested&postcss-mixins

在刚开始使用PostCSS时,我就想到要用PostCSS实现我在Sass中最常用的特性。所以,我找到了postcss-nestedpostcss-mixins。将它们结合起来后,就可以做到这样:

@define-mixin clearfix{    &:after{        display: table;        clear: both;        content: " ";    }}.column-container{    color: #333;    @mixin clearfix;}

编译后:

.column-container{    color: #333;}.column-container:after{    display: table;    clear: both;    content: " ";}

到这里,你是否已经有了“预编译器可以做到的它也可以做到”的感觉呢?

如何使用PostCSS

我个人推荐结合使用,本文在此只介绍的用法。

-postcss及插件都是,首先,使用npm install将它们分别安装到项目目录中(会位于node_modules)。然后,编辑glupfile.js,将PostCSS注册为Gulp的一个任务。以下是一个结合使用了Autoprefixerpostcss-simple-varspostcss-mixinspostcss-nested4个插件,且生成source map文件的例子:

var gulp = require("gulp");var postcss = require("gulp-postcss");var autoprefixer = require('autoprefixer-core');var postcssSimpleVars = require("postcss-simple-vars");var postcssMixins = require("postcss-mixins");var postcssNested = require("postcss-nested");var sourcemaps = require("gulp-sourcemaps");// Css process.gulp.task("postcss", function(){    var processors = [        postcssMixins,        postcssSimpleVars,        postcssNested,        autoprefixer({            browsers: ["Android 4.1", "iOS 7.1", "Chrome > 31", "ff > 31", "ie >= 10"]        })];    return gulp.src(["./stylesheets/src/*.css"])        .pipe(sourcemaps.init())        .pipe(postcss(processors))        .pipe(sourcemaps.write("."))        .pipe(gulp.dest("./stylesheets/dest"));});

在上面这段中,processors是一个数组,定义了用到的PostCSS插件。PostCSS会按照定义顺序依次执行插件,因此,在结合多个插件使用时,请注意它们的位置。

自定义转换

此外,你可以很容易地创建你自己的转换(还记得前面说过PostCSS的插件都是函数吧?)。例如,下面是一个自定义的转换方法,它将css代码中的带有单位的值,更改为px的值。

var custom = function(css, opts){    css.eachDecl(function(decl){        decl.value = decl.value.replace(/\d+rem/, function(str){            return 16 * parseFloat(str) + "px";        });    });};

然后,你将这个方法直接添加到processors中(就像postcssMixins那些那样)就可以使用。如果原来有值是3rem,将变成48px

更多:

转载地址:http://unogi.baihongyu.com/

你可能感兴趣的文章
SpringBoot 入门:环境搭建与HelloWorld~
查看>>
SpringBoot 入门:整合MongoDB,做简单查找功能
查看>>
自学大话设计模式——简单工厂模式与策略模式
查看>>
读Java核心技术36讲有感——谈谈对Java的理解,谈谈Exception和Error
查看>>
foobar2000中文版官方下载【多功能的音频播放器】
查看>>
【分析微软为何延迟发布Win8.1】
查看>>
【德国磁盘清理●V4.1.0 中文版】磁盘碎片整理工具
查看>>
【详细解析Windows7系统防火墙设置】
查看>>
【教你完美解决Windows7系统乱码问题】
查看>>
金馆长qq表情包●绿色版【金馆长表情252个】
查看>>
卡巴斯基安全部队2013中文免费版【可免费使用一个月】
查看>>
【Win8网络被攻击教你手动绑定ARP技巧】
查看>>
【让U盘的使用记录在注册表中消失的方法】
查看>>
【找回Win8系统不见的附件目录&小工具】
查看>>
【教你几种禁止修改IP的方法】
查看>>
思量QQ本地会员v3.8官方2013版【免费使用部分QQ会员功能】
查看>>
【U盘修复工具的使用教程】
查看>>
【问答解题:xp无法连接wpa加密怎么办?】
查看>>
【Win7系统 沙滩啤酒桌面主题】
查看>>
【.NET支持上下左右移动操作】
查看>>