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.
/* before */
$blue: #056ef0;
$column: 200px;
.menu {
width: calc(4 * $column);
}
.menu_link {
background: $blue;
width: $column;
}
/* after */
.menu {
width: calc(4 * 200px);
}
.menu_link {
background: #056ef0;
width: 200px;
}
/* before */
.notice--clear {
@if 3 < 5 {
background: green;
}
@else {
background: blue;
}
}
/* after */
.notice--clear {
background: green;
}
/* before */
@for $i from 1 to 3 {
.b-$i { width: $(i)px; }
}
/* after */
.b-1 {
width: 1px
}
.b-2 {
width: 2px
}
.b-3 {
width: 3px
}
/* before */
@each $icon in (foo, bar, baz) {
.icon-$(icon) {
background: url('icons/$(icon).png');
}
}
/* after */
.icon-foo {
background: url('icons/foo.png');
}
.icon-bar {
background: url('icons/bar.png');
}
.icon-baz {
background: url('icons/baz.png');
}
/* before */
@define-mixin icon $name {
padding-left: 16px;
&::after {
content: "";
background-url: url(/icons/$(name).png);
}
}
.search {
@mixin icon search;
}
/* after */
.search {
padding-left: 16px;
}
.search::after {
content: "";
background-url: url(/icons/search.png);
}
/* before */
@define-extend bg-green {
background: green;
}
.notice--clear {
@extend bg-green;
}
/* after */
.notice--clear {
background: green;
}
/* Before */
@import "partials/base"; /* Contents of partials/_base.css: `body { background: black; }` */
/* After */
body { background: black; }
/* Before */
.heading {
margin: 20px;
padding: @margin;
}
/* After */
.heading {
margin: 20px;
padding: 20px;
}
/* Before */
.parent {
background: white;
@at-root{
.child {
background: black;
}
}
}
/* After */
.child {
background: black;
}
.parent {
background: white;
}
Follow these simple steps to use PreCSS.
Add PreCSS to your build tool:
npm install precss --save-dev
Add the PostCSS SCSS parser to your build tool:
npm install postcss-scss --save-dev
require('precss')({ /* options */ }).process(YOUR_CSS, { parser: require('postcss-scss') });
Add PostCSS to your build tool:
npm install postcss --save-dev
Load PreCSS as a PostCSS plugin:
postcss([
require('precss')({ /* options */ })
]).process(YOUR_CSS, { parser: require('postcss-scss') }).then(function (result) {
// do something with result.css
});
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Enable PreCSS within your Gulpfile:
var postcss = require('gulp-postcss');
gulp.task('css', function () {
return gulp.src('./css/src/*.css').pipe(
postcss([
require('precss')({ /* options */ })
])
).pipe(
gulp.dest('./css')
);
});
Add Grunt PostCSS to your build tool:
npm install precss --save-dev
Enable PreCSS within your Gruntfile:
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
parser: require('postcss-scss'),
processors: [
require('precss')({ /* options */ })
]
},
dist: {
src: 'css/*.css'
}
}
});
PreCSS blends Sass-like strength with W3C future-syntax superpower, powered by the following plugins (in this order):
- postcss-partial-import: W3C and Sass-like imports
- postcss-mixins: Sass-like mixins
- postcss-advanced-variables: Sass-like variables and methods
- postcss-custom-selectors: W3C custom selectors
- postcss-custom-media: W3C custom media queries
- postcss-custom-properties: W3C custom variables
- postcss-media-minmax: W3C
<
<=
>=
>
media queries - postcss-color-function: W3C color methods
- postcss-nesting: W3C nested selectors
- postcss-nested: Sass-like nested selectors
- postcss-atroot: place rules back up to the root
- postcss-property-lookup: reference other property values
- postcss-extend: W3C and Sass-like extend methods
- postcss-selector-matches: W3C multiple matches pseudo-classes
- postcss-selector-not: W3C multiple not pseudo-classes