# PostCSS
<img align="right" width="95" height="95" src="http://ai.github.io/postcss/logo.png" title="Philosopher's stone, logo of PostCSS">
PostCSS is a framework for CSS postprocessors,
to modify CSS with JavaScript with full source map support.
It takes care of most common CSS tool tasks:
1. parses CSS;
2. gives you usable JS API to edit CSS node tree;
3. dumps modified node tree into CSS string;
4. generates (or modifies existent) source map for your changes;
You can use this framework to write you own:
* CSS minifier or beautifier.
* CSS polyfills.
* Grunt plugin to generate sprites, include `data-uri` images
or any other works.
* Text editor plugin to automate CSS routine.
* Command-line CSS tool.
Sponsored by [Evil Martians](http://evilmartians.com/).
## Built with PostCSS
* [Autoprefixer] adds vendor prefixes by Can I Use data.
* [grunt-pixrem], `rem` unit polyfill.
* [CSS MQPacker] joins same media queries.
* [RTLCSS] mirrors styles for right-to-left locales.
* [CSSWring] and [grunt-csswring] CSS minifier with full source map support.
* [Pleeease] is a pack of various postprocessors.
* [Grunt-webpcss] to duplicate images in CSS to WebP for supported browsers.
* [Pleeease Filters] converts WebKit filters to SVG filter for other browsers.
* [css2modernizr] analyzes your CSS and output only used Modernizr’s settings.
* [data-separator] splits data-uri into a separate CSS file.
[Pleeease Filters]: https://github.com/iamvdo/pleeease-filters
[grunt-csswring]: https://github.com/princed/grunt-csswring
[data-separator]: https://github.com/Sebastian-Fitzner/grunt-data-separator
[css2modernizr]: https://github.com/vovanbo/css2modernizr
[Grunt-webpcss]: https://github.com/lexich/grunt-webpcss
[Autoprefixer]: https://github.com/ai/autoprefixer
[grunt-pixrem]: https://github.com/robwierzbowski/grunt-pixrem
[CSS MQPacker]: https://github.com/hail2u/node-css-mqpacker
[Pleeease]: http://pleeease.io/
[CSSWring]: https://github.com/hail2u/node-csswring
[RTLCSS]: https://github.com/MohammadYounes/rtlcss
## Quick Example
Let’s fix forgotten `content` property in `::before` and `::after`:
```js
var postcss = require('postcss');
var contenter = postcss(function (css) {
css.eachRule(function (rule) {
if ( rule.selector.match(/::(before|after)/) ) {
// In every ::before/::after rule
// Did we forget content property?
var good = rule.some(function (i) { return i.prop == 'content'; });
if ( !good ) {
// Add content: "" if we forget it
rule.prepend({ prop: 'content', value: '""' });
}
}
});
});
```
And then CSS with forgotten `content`:
```css
a::before {
width: 10px;
height: 10px
}
```
will be fixed by our new `contenter`:
```js
var fixed = contenter.process(css).css;
```
to:
```css
a::before {
content: "";
width: 10px;
height: 10px
}
```
## Features
### Source Map
PostCSS generates source map for its changes:
```js
result = processor.process(css, { map: true, from: 'from.css', to: 'to.css' });
result.css // String with processed CSS
result.map // Source map
```
And modifies source map from previous step (like Sass preprocessor):
```js
var sass = compiler.compile(sass);
processor.process(sass.css, {
map: { prev: sass.map },
from: 'from.sass.css',
to: 'to.css'
});
```
### Preserves code formatting and indentations
PostCSS will not change any byte of a rule if you don’t modify its node:
```js
postcss(function (css) { }).process(css).css == css;
```
And when you modify CSS nodes, PostCSS will try to copy coding style:
```js
contenter.process("a::before{color:black}")
// a::before{content:'';color:black}
contenter.process("a::before {\n color: black;\n }")
// a::before {
// content: '';
// color: black;
// }
```
It allows to use PostCSS in text editor plugin and preserve user code style.
## Why PostCSS Better Than …
### Preprocessors
Preprocessors (like Sass or Stylus) give us special language with variables,
mixins, statements and compile it to CSS. Compass, nib and other mixins
libraries use these languages to work with prefixes, sprites and inline images.
But Sass and Stylus languages were created to be syntax-sugar for CSS.
Writing really complicated programs using preporcessor languages
is very difficult. [Autoprefixer] is absolutely impossible to implement
on top of Sass.
PostCSS gives you comfort and power of JS or CoffeeScript to working with CSS.
You can do really magic things with wide range of [npm] libraries.
But postprocessors are not enemies for preprocessors. Sass and Stylus are still
the best way to improve readability and add some syntax sugar to CSS.
You can easily combine preprocessors and postprocessors
(and PostCSS will also update source map from Sass or Stylus).
[Autoprefixer]: https://github.com/ai/autoprefixer
[npm]: https://npmjs.org/
### RegExp
Some Grunt plugins modify CSS with regular expressions but using a CSS parser
and a node tree is a much safer way to edit CSS. Also, regexps will break
source maps generated by preprocessors.
### CSS Parsers
There are a lot of good CSS parsers, like [Gonzales]. But they help you only
with first step.
Unlike them PostCSS gives you full source map support and useful high level API
(for example, safe iterators).
[Gonzales]: https://github.com/css/gonzales
### Rework
[Rework] and PostCSS are very similar, but they has different targets.
Rework was created to build new CSS sublanguage to replace Stylus (like [Myth]).
PostCSS was created for CSS tools, which works in chain with legacy CSS code
(like Autoprefixer).
Because of this background difference, PostCSS:
* Better works with source map, because it should update map from previous step
(like Sass compiling).
* Saves all your spaces and code style, because it can be worked in text editor
plugins.
* Has safer parser, because it can be used for legacy code. Only PostCSS can
parse all hacks from [Browserhacks.com](http://browserhacks.com/).
* Has high level API to clean your processor from common tasks.
[Myth]: http://www.myth.io/
[Rework]: https://github.com/visionmedia/rework
## Usage
You can parse CSS by `postcss.parse()` method, which returns CSS AST:
```js
var postcss = require('postcss');
var css = postcss.parse('a { color: black }');
```
Then you can change this AST. Use `css.list` to get childs.
Properties `rule.selector`, `decl.prop`, `decl.value`, `atrule.name`
and `atrule.params` contain data.
Don’t use underscore properties (like `_selector`, `_params` and `_value`),
because they are only for comments save magic
(See [Raw Properties](#raw-properties) below). Use getters and setters instead
(like `selector`, `selectors`, `params` and `value`).
```js
css.list[0].value = 'white';
```
After changes you can get new CSS and modification’s source map:
```js
var result = css.toResult(options);
result.css //=> 'a { color: white }'
result.map //=> '{"version":3, … }'
```
Methods `postcss.parse()` and `CSS#toResult()` are low level API, for most cases
it will be better to create processors with simplier API and chaining.
### Processor
The function `postcss(fn)` creates a processor from your function:
```js
var postcss = require('postcss');
var processor = postcss(function (css) {
// Code to modify CSS
});
```
If you want to combine multiple processors (and parse CSS only once),
you can add several functions using the `use(fn)` method:
```js
var all = postcss().
use(prefixer).
use(minifing);
```
Processor function can change the current CSS node tree:
```js
postcss(function (css) {
css.append( /* new rule */ )
});
```
or create a completely new CSS root node and return it instead:
```js
postcss(function (css) {
var newCSS = postcss.root()
// Add rules and declarations
return newCSS;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Sublime Text 3(40多插件+4主题) (2000个子文件)
imesupport_hook.c 4KB
benchmark-native.c 613B
benchmark-native.c 613B
integral.css 3KB
integral.css 3KB
integral.expected.css 3KB
integral.expected.css 3KB
complex.expected.css 1KB
complex.expected.css 1KB
complex.css 1KB
complex.css 1KB
issue-94-3.expected.css 539B
issue-94-3.expected.css 539B
already-aligned.css 527B
already-aligned.expected.css 527B
already-aligned.css 527B
already-aligned.expected.css 527B
issue-94-3.css 508B
issue-94-3.css 508B
value-align.expected.css 464B
value-align.expected.css 464B
value-align.css 450B
value-align.css 450B
issue-94-1.expected.css 411B
issue-94-1.expected.css 411B
issue-94-1.css 407B
issue-94-1.css 407B
test-2.expected.css 342B
test-2.expected.css 342B
test-3.expected.css 331B
test-3.expected.css 331B
test.css 299B
test.css 299B
test.expected.css 270B
test.expected.css 270B
test-3.expected.css 258B
test-3.expected.css 258B
test-3.expected.css 258B
test-3.expected.css 258B
issue-94-2.expected.css 256B
issue-94-2.css 256B
issue-94-2.expected.css 256B
issue-94-2.css 256B
test-2.expected.css 222B
test-2.expected.css 222B
test-2.expected.css 222B
test-2.expected.css 222B
test.css 202B
test.css 202B
test.css 202B
test.css 202B
leftovers-2.expected.css 200B
leftovers-1.expected.css 200B
leftovers-2.expected.css 200B
leftovers-1.expected.css 200B
test.expected.css 198B
test.expected.css 198B
test.expected.css 198B
test.expected.css 198B
leftovers-4.expected.css 197B
leftovers-4.css 197B
leftovers-3.css 197B
leftovers-1.css 197B
leftovers-3.expected.css 197B
leftovers-2.css 197B
leftovers-4.expected.css 197B
leftovers-4.css 197B
leftovers-3.css 197B
leftovers-1.css 197B
leftovers-3.expected.css 197B
leftovers-2.css 197B
same-name.expected.css 180B
same-name.expected.css 180B
test.css 166B
test.css 166B
test.expected.css 166B
test.css 166B
test.css 166B
test.css 166B
test.css 166B
test.expected.css 166B
test-2.expected.css 165B
test-3.expected.css 165B
test-2.expected.css 165B
test-3.expected.css 165B
test.css 164B
test.css 164B
issue-232.css 158B
issue-232.css 158B
same-name.css 156B
same-name.css 156B
issue-232.expected.css 154B
issue-232.expected.css 154B
both.expected.css 148B
both.expected.css 148B
both.css 140B
both.css 140B
data-uri.expected.css 137B
data-uri.css 137B
data-uri.expected.css 137B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- 山海纵横2017-02-19语法选择html的时候,为什么不能提示下面html的类名class了呢,选择有的语法可以提示,我的css,和html还有js都是写在一个页面里面的,没有分开单独文件写,有什么设置么?
- joinandjoin2018-11-09谢谢,不错,
h414885835
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- SAP常用事务代码,涉及到MM板块
- A Pathfinding Project Pro v5.0.5.unitypackage
- 糖尿病并发症预警数据集
- All In 1 Sprite Shader v3.6.unitypackage
- 基于深度学习的电影推荐系统源代码,数据集采用MovieLens1M,实现了AE、VAE、BERT提取电影名特征3种方法
- 联通支付注册-滑动验证-selenium+Opencv 自动化测试程序-pc端
- UGUI Super ScrollView v2.5.4.unitypackage
- 锅炉腐蚀炉管穿孔 炉水发红蒸汽冷凝水铁超标一招化解.docx
- 从效率角度看6种MySQL批量更新方式的优劣.zip
- Behavior Designer - Behavior Trees for Everyone v1.7.9.unitypack
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功