npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

postcss-logical-polyfill

v0.6.1

Published

A PostCSS plugin that provides physical property polyfills for CSS logical properties with intelligent direction-aware selector handling, block-direction optimization, and extended logical property support via shim system

Readme

postcss-logical-polyfill

NPM Version Build Status Coverage Status NPM Downloads Types Package Size

A PostCSS plugin that transforms CSS logical properties into physical properties with appropriate direction selectors, enabling backward compatibility for older browsers.

Quick Start

Installation

# Using npm
npm install postcss-logical-polyfill --save-dev
# Using pnpm
pnpm add -D postcss-logical-polyfill
# Using yarn
yarn add -D postcss-logical-polyfill

Basic Usage

Playground

Full document

// postcss.config.js
module.exports = {
  plugins: [
    require('postcss-logical-polyfill')()
  ]
}

Example Transformation

Input CSS:

.container {
  margin-inline: 1rem;
  padding-block: 2rem;
  border-inline-start: 2px solid blue;
}

Output CSS:

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

Key Features

  • 🔄 Polyfill Direction: Transforms logical properties → physical properties (reverse of most tools)
  • 🎯 Smart Generation: Creates both LTR and RTL versions automatically
  • ⚡ Optimized Output: Block-direction properties generate single rules (no duplication)
  • 🔗 Extended Support: Includes scroll properties and logical values via integrated shim
  • 🧪 Experimental Features: Linear gradient logical directions and draft CSS specs
  • 🎛️ Configurable: Custom selectors and output order control
  • 🏗️ Framework Ready: Works with any build tool or CSS framework

Why Use This Plugin?

While modern browsers support CSS logical properties, older browsers don't. This plugin acts as a polyfill, converting your modern logical properties to physical properties that work everywhere, while preserving the directional behavior for international layouts.

Perfect for:

  • ✅ Supporting older browsers while using modern CSS
  • ✅ Gradual migration from physical to logical properties
  • ✅ RTL/LTR internationalization
  • ✅ Framework integration with directional layouts

Installation

# Using npm
npm install postcss-logical-polyfill --save-dev

# Using pnpm
pnpm add -D postcss-logical-polyfill

# Using yarn
yarn add -D postcss-logical-polyfill

What It Does

This plugin transforms CSS Logical Properties into physical properties with appropriate direction selectors for browser compatibility. It intelligently processes:

  • All standard logical properties (margin, padding, border, inset, sizing, etc.)
  • Logical values (float: inline-start, clear: inline-end, resize: block)
  • Scroll properties (scroll-margin, scroll-padding)
  • Experimental features (linear-gradient logical directions)
  • Both scoped and unscoped logical properties

➡️ Complete supported properties list

Configuration

Basic Options

const logicalPolyfill = require('postcss-logical-polyfill');

postcss([
  logicalPolyfill({
    // Direction selectors (default shown)
    rtl: { selector: '[dir="rtl"]' },
    ltr: { selector: '[dir="ltr"]' },
    
    // Output order for unscoped properties
    outputOrder: 'ltr-first'  // or 'rtl-first'
  })
])

➡️ Complete configuration guide

Example Transformation

Input CSS:

/* Unscoped logical properties - will generate both LTR and RTL versions */
.container {
  margin-inline: 1rem;
  padding-inline-start: 1rem;
}

/* Block-direction properties - Generate single optimized rule */
.content {
  margin-block: 2rem;
  padding-block-start: 1rem;
}

/* Extended logical properties via shim system */
.scroll-area {
  scroll-margin-inline: 10px;
  float: inline-start;
}

/* Experimental: Linear gradient logical directions */
.gradient-element {
  background: linear-gradient(to inline-end, red, blue);
}

Output CSS:

.container {
  margin-left: 1rem;
  margin-right: 1rem;
}
[dir="ltr"] .container {
  padding-left: 1rem;
}
[dir="rtl"] .container {
  padding-right: 1rem;
}

/* Block-direction properties - Single optimized rule */
.content {
  margin-top: 2rem;
  margin-bottom: 2rem;
  padding-top: 1rem;
}

/* Extended logical properties transformed */
.scroll-area {
  scroll-margin-left: 10px;
  scroll-margin-right: 10px;
}
[dir="ltr"] .scroll-area {
  float: left;
}
[dir="rtl"] .scroll-area {
  float: right;
}

/* Experimental features automatically enabled */
[dir="ltr"] .gradient-element {
  background: linear-gradient(to right, red, blue);
}
[dir="rtl"] .gradient-element {
  background: linear-gradient(to left, red, blue);
}

How It Works

This plugin intelligently processes CSS through a 7-phase optimization pipeline:

  1. 🔍 Detection: Identifies logical properties and existing direction selectors
  2. 🎯 Classification: Separates block-direction, inline-direction, and mixed properties
  3. 🔄 Transformation: Converts logical to physical properties based on direction context
  4. 🎯 Selector Application: Adds appropriate direction selectors when needed
  5. 🔧 Optimization: Merges rules and eliminates redundant declarations
  6. 🎯 Smart Priority: Implements rightmost selector precedence for predictable behavior
  7. ✨ Output: Generates clean, optimized CSS for maximum compatibility

➡️ Detailed technical explanation

Getting Started

Installation

npm install postcss-logical-polyfill --save-dev

Basic Setup

// postcss.config.js
module.exports = {
  plugins: [
    require('postcss-logical-polyfill')()
  ]
}

Build Tool Integration

➡️ Integration guides for Webpack, Vite, Next.js, and more

Important Notes

HTML Direction Attribute Required

You must set the dir attribute on your HTML for the generated CSS to work:

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

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

Custom Selectors

You can configure custom direction selectors for your framework:

logicalPolyfill({
  ltr: { selector: '.ltr' },
  rtl: { selector: '.rtl' }
})

➡️ Complete usage guide and best practices

Examples

This package includes ready-to-run examples for different build systems and use cases.

# View all available examples
ls examples/

# Run specific examples
cd examples/basic && npx tsx process.ts
cd examples/webpack && npx tsx process.ts
cd examples/sass && npx tsx process.ts

# Run all examples at once
pnpm run examples

Available examples:

  • Basic: Plain CSS with PostCSS
  • Build Tools: Webpack integration
  • Preprocessors: SASS and LESS integration
  • Configuration: Output order and selector priority
  • CLI: PostCSS command-line usage

➡️ View all examples

Troubleshooting

Having issues? Check our troubleshooting guide for common problems and solutions.

➡️ Complete troubleshooting guide

Documentation

Learn More

Requirements

  • Node.js 16.0.0 or later
  • PostCSS 8.0.0 or later

Contributing

Contributions are welcome! Please see our contributing guidelines for details.

Credits

This plugin wraps and extends postcss-logical to provide polyfill functionality.

License

MIT