PostCSS Number Functions lets you use Sass Number Functions in CSS, which
include abs()
, ceil()
, floor()
, max()
, min()
, percentage()
,
random()
and round()
.
.example {
width: floor(500px / 3);
}
/* becomes */
.example {
width: 166px;
}
The abs()
function returns a number with its absolute value, preserving
its CSS unit.
The ceil()
function returns a number rounded up to a whole number, preserving
its CSS unit.
The floor()
function returns a number rounded down to a whole number,
preserving its CSS unit.
The max()
function returns the maximum value of several numbers separated by
commas, preserving its CSS unit.
The min()
function returns the minimum value of several numbers separated by
commas, preserving its CSS unit.
The percentage()
function returns the percentage from a number, transforming
its CSS unit into %
.
The random()
function returns a random, unitless number between 0 and 1,
including 0 but not including 1. If it is passed one number then it returns a
random number between 0 and that number, including both numbers and preserving
the unit of the argument. If it is passed two arguments then it returns a
random number between those numbers, including both numbers and preserving the
unit of the later argument.
The floor()
function returns a number rounded to the nearest whole number,
preserving its CSS unit.
Add PostCSS Number Functions to your build tool:
npm install postcss-number-functions --save-dev
Use PostCSS Number Functions to process your CSS:
import PostCSSNumberFunctions from 'postcss-number-functions';
PostCSSNumberFunctions.process(YOUR_CSS);
Add PostCSS to your build tool:
npm install postcss --save-dev
Use PostCSS Number Functions as a plugin:
import postcss from 'gulp-postcss';
import PostCSSNumberFunctions from 'postcss-number-functions';
postcss([
PostCSSNumberFunctions()
]).process(YOUR_CSS);
Add Gulp PostCSS to your build tool:
npm install gulp-postcss --save-dev
Use PostCSS Number Functions in your Gulpfile:
import postcss from 'gulp-postcss';
import PostCSSNumberFunctions from 'postcss-number-functions';
gulp.task('css', () => gulp.src('./src/*.css').pipe(
postcss([
PostCSSNumberFunctions()
])
).pipe(
gulp.dest('.')
));
Add Grunt PostCSS to your build tool:
npm install grunt-postcss --save-dev
Use PostCSS Number Functions in your Gruntfile:
import PostCSSNumberFunctions from 'postcss-number-functions';
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
use: [
PostCSSNumberFunctions()
]
},
dist: {
src: '*.css'
}
}
});