Skip to content

Installation

PostCSS Logical Polyfill can be installed using your preferred package manager and integrated into any PostCSS workflow.

  • Node.js 16.0.0 or later
  • PostCSS 8.0.0 or later
Terminal window
npm install postcss-logical-polyfill --save-dev
Terminal window
pnpm add -D postcss-logical-polyfill
Terminal window
yarn add -D postcss-logical-polyfill

After installation, add the plugin to your PostCSS configuration:

module.exports = {
plugins: [
'postcss-logical-polyfill'
// same as the following witout any options
// require('postcss-logical-polyfill')()
]
}
import logicalPolyfill from 'postcss-logical-polyfill';
export default {
plugins: [
logicalPolyfill()
]
};
{
"postcss": {
"plugins": {
"postcss-logical-polyfill": {}
}
}
}

You can configure the plugin behavior by passing options:

postcss.config.js
module.exports = {
plugins: [
require('postcss-logical-polyfill')({
// Direction selectors (default shown)
rtl: { selector: '[dir="rtl"]' },
ltr: { selector: '[dir="ltr"]' },
// Output order for unscoped properties
outputOrder: 'ltr-first' // or 'rtl-first'
})
]
}

See the Configuration Guide for detailed configuration options.

Important: For the generated CSS to work correctly, you must set the dir attribute on your HTML:

<!DOCTYPE html>
<html dir="ltr"> <!-- For left-to-right layouts -->
<head>
<title>My Website</title>
</head>
<body>
<!-- Your content -->
</body>
</html>

For right-to-left layouts:

<html dir="rtl"> <!-- For right-to-left layouts -->

Without the dir attribute, the generated [dir="ltr"] and [dir="rtl"] selectors won’t match any elements.

To verify the plugin is working correctly, create a simple CSS file with logical properties:

input.css
.test {
margin-inline: 1rem;
padding-block: 2rem;
border-inline-start: 1px solid red;
}

After processing, you should see output like:

output.css
.test {
margin-left: 1rem;
margin-right: 1rem;
padding-top: 2rem;
padding-bottom: 2rem;
}
[dir="ltr"] .test {
border-left: 1px solid red;
}
[dir="rtl"] .test {
border-right: 1px solid red;
}

If you’re having issues with installation or setup, check the Troubleshooting Guide for common problems and solutions.

Now that you have PostCSS Logical Polyfill installed, check out: