1.1.0 • Published 8 years ago
postcss-center v1.1.0
postcss-center
PostCSS plugin to center elements.
Introduction
Centering elements in CSS isn't exactly straight-forward, but we can change that!
.foo {
top: center;
left: center;
}Transpiles into:
.foo {
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}Of course, you don't have to include both top and left:
.foo {
top: center;
}Transpiles into:
.foo {
position: absolute;
top: 50%;
transform: translateY(-50%);
}Or...
.foo {
left: center;
}Transpiles into:
.foo {
position: absolute;
left: 50%;
margin-right: -50%;
transform: translateX(-50%);
}That's about it!
Conditions
- If the value of
toporleftis notcenterit will be preserved. If both are notcenter, this plugin will do nothing! - If the rule already has a
positionit will only be preserved if its value isrelativeorfixed. All other values will be replaced withabsolute. - If the rule has a
positionofrelativeor the value ofleftis notcenter, themargin-rightdeclaration will not be inserted.
Installation
$ npm install postcss-centerUsage
JavaScript
postcss([ require('postcss-center') ]);TypeScript
import * as postcssCenter from 'postcss-center';
postcss([ postcssCenter ]);Options
None at this time.
Testing
Run the following command:
$ npm testThis will build scripts, run tests and generate a code coverage report. Anything less than 100% coverage will throw an error.
Watching
For much faster development cycles, run the following commands in 2 separate processes:
$ npm run build:watchCompiles TypeScript source into the ./dist folder and watches for changes.
$ npm run watchRuns the tests in the ./dist folder and watches for changes.