Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postcss-prefixwrap

dbtedman287.7kMIT1.55.0TypeScript support: included

A PostCSS plugin that is used to wrap css styles with a css selector to constrain their affect on parent elements in a page.

css, javascript, nodejs, pnpm, postcss, postcss-plugin, typescript, yarn

readme

PostCSS Prefix Wrap

CI GitHub Pipeline SAST GitHub Pipeline Latest Release NPM Downloads Per Week

A PostCSS (postcss.org) plugin which prepends a selector to CSS styles to constrain their effect on parent elements in a page.

Supports Versions
Bun (bun.sh) latest
Deno (deno.com) v2
NodeJS (nodejs.org) v18, v19, v20, v21, v22
PostCSS (postcss.org) v7, v8

⚠️ PostCSS v7 support is no longer validated in automated test cases, and will be removed entirely in a future release.

How to use this plugin?

⚠️ These instructions are only for this plugin. See the PostCSS (postcss.org) website for framework information.

Install

Package Manager or Runtime Command
Bun (bun.sh) bun add postcss-prefixwrap --dev --exact
Deno (deno.com) deno add npm:postcss-prefixwrap --dev
NPM (npmjs.com) npm install postcss-prefixwrap --save-dev --save-exact
PNPM (pnpm.io) pnpm add postcss-prefixwrap --save-dev --save-exact
Yarn (yarnpkg.com) yarn add postcss-prefixwrap --dev --exact

Configure

Add to your PostCSS (postcss.org) configuration.

const PostCSS = require("gulp-postcss");
const PrefixWrap = require("postcss-prefixwrap");

PostCSS([PrefixWrap(".my-custom-wrap")]);

Container

Add the container to your markup.

<div class="my-custom-wrap"><!-- Your existing markup. --></div>

View

View your CSS, now prefix-wrapped.

Before

p {
    color: red;
}

body {
    font-size: 16px;
}

After

.my-custom-wrap p {
    color: red;
}

.my-custom-wrap {
    font-size: 16px;
}

What options does it have?

PrefixWrap(".my-custom-wrap", {
    // You may want to exclude some selectors from being prefixed, this is
    // enabled using the `ignoredSelectors` option.
    ignoredSelectors: [":root", "#my-id", /^\.some-(.+)$/],

    // You may want root tags, like `body` and `html` to be converted to
    // classes, then prefixed, this is enabled using the `prefixRootTags`
    // option.
    // With this option, a selector like `html` will be converted to
    // `.my-container .html`, rather than the default `.my-container`.
    prefixRootTags: true,

    // In certain scenarios, you may only want `PrefixWrap()` to wrap certain
    // CSS files. This is done using the `whitelist` option.
    // ⚠️ **Please note** that each item in the `whitelist` is parsed as a
    // regular expression. This will impact how file paths are matched when you
    // need to support both Windows and Unix like operating systems which use
    // different path separators.
    whitelist: ["editor.css"],

    // In certain scenarios, you may want `PrefixWrap()` to exclude certain CSS
    // files. This is done using the `blacklist` option.
    // ⚠️ **Please note** that each item in the `blacklist` is parsed as a
    // regular expression. This will impact how file paths are matched when you
    // need to support both Windows and Unix like operating systems which use
    // different path separators.
    // If `whitelist` option is also included, `blacklist` will be ignored.
    blacklist: ["colours.css"],

    // When writing nested css rules, and using a plugin like `postcss-nested`
    // to compile them, you will want to ensure that the nested selectors are
    // not prefixed. This is done by defining the `nested` property and setting
    // the value to the selector prefix being used to represent nesting, this is
    // most likely going to be `"&"`.
    nested: "&",
});

What problems can it solve?

PostCSS Prefix Wrap can be used to solve multiple different problems. The following articles give some concrete examples:

How to contribute?

Read our Contributing Guide to learn more about how to contribute to this project.

Is this project secure?

Read our Security Guide to learn how security is considered during the development and operation of this plugin.

License

The MIT License is used by this project.

changelog

PostCSS Prefix Wrap // Changelog

Releases ordered so that the most recent are displayed at the top, with the currently being developed release at the top, labeled as 🚧 In Development. This release will be given a number once it is ready to be released. Each release can contain both a 💡 Features and Improvements, 🔧 Maintenance, and 🐛 Bug Fixes sections.

1.55.0

🔧 Maintenance

  • Apply dependency updates.

1.54.0

🔧 Maintenance

  • Apply dependency updates.

1.53.0

💡 Features and Improvements

🔧 Maintenance

  • Apply dependency updates.

1.52.0

🔧 Maintenance

  • Apply dependency updates.

1.51.0

💡 Features and Improvements

  • Use real types from plugin source.

🐛 Bug Fixes

  • #515 Correctly handle :where statement.

1.50.0

🔧 Maintenance

  • Apply dependency updates.

1.49.0

💡 Features and Improvements

  • NodeJS v22 Support

🔧 Maintenance

  • Apply dependency updates.
  • Simplify SAST workflows, focusing on GitHub embedded tools where possible.

1.48.0

🔧 Maintenance

  • Apply dependency updates.

1.47.0

💡 Features and Improvements

1.46.0

🔧 Maintenance

  • Apply dependency updates.

1.45.0

🔧 Maintenance

  • Apply dependency updates.

1.44.0

💡 Features and Improvements

  • Rethink testing strategy.
  • Add PNPM support to Dependabot.
  • Add Semgrep SAST.

🔧 Maintenance

  • Drop support for unmaintained NodeJS versions according to Release Schedule (nodejs.org).
  • Drop support for postcss v7 in test cases as it includes vulnerabilities that won't be fixed.
  • Apply dependency updates.

1.43.0

🔧 Maintenance

  • NodeJS Update
  • Dependency Updates

1.42.0

🔧 Maintenance

  • NodeJS Update
  • Dependency Updates

1.41.0

🔧 Maintenance

  • NodeJS Update
  • Dependency Updates

1.40.0

🔧 Maintenance

  • Fix TypeScript config for updated NodeJS support.
  • Update use of Jest directives to definitions file.
  • Update import formats.
  • Dependency Updates

1.39.1

🔧 Maintenance

  • Dependency Updates

1.39.0

💡 Features and Improvements

  • OSV Scanning in SAST Pipeline
  • Link to external articles for What problems can it solve?
  • Include Node v19 support

🔧 Maintenance

  • Dependency Updates

1.38.1

🐛 Bug Fixes

  • Fix repository badges

1.38.0

🔧 Maintenance

  • Dependency Updates

1.37.0

💡 Features and Improvements

  • OpenSSF Scorecard
  • Rethink focus on Code Coverage, instead focus on developing with TDD

1.36.0

💡 Features and Improvements

  • #187 Integration Test Rethink

🐛 Bug Fixes

  • #189 Fix typescript broken import path

1.35.1

🐛 Bug Fixes

  • #185 Don't use alias * selector as will break imports when used outside of config without it.

1.35.0

💡 Features and Improvements

  • Structure Refactor

🔧 Maintenance

  • Dependency Updates

1.34.0

💡 Features and Improvements

  • #180 Escape prefixSelector

🔧 Maintenance

  • Dependency Updates
  • Revised SAST Configuration

1.33.0

🐛 Bug Fixes

  • #176 Update instructions on whitelist and blacklist to clarify regular expression usage
  • Expand CI support to run on windows host to assist in identifying cross-platform issues

1.32.0

🔧 Maintenance

  • Dependency Updates

1.31.0

💡 Features and Improvements

  • Updated README.md documentation

🔧 Maintenance

  • Dependency Updates

1.30.1

💡 Features and Improvements

  • Snyk + CodeQL SAST
  • Switch to PNPM from Yarn
  • Introduce Makefile for common commands

🐛 Bug Fixes

  • :root missing from list of root tags

1.29.1

🔧 Maintenance

  • Dependency Updates

1.29.0

💡 Features and Improvements

  • [154] Add support for nested selectors with new configuration option

1.28.1

🐛 Bug Fixes

  • [146] Fix Rule not wrapped when the first selector starts with the prefix

1.27.0

🔧 Maintenance

  • Dependency Updates

1.26.0

🔧 Maintenance

  • Dependency Updates

1.25.0

🔧 Maintenance

  • Dependency Updates

1.24.0

🔧 Maintenance

  • Dependency Updates

1.23.0

💡 Features and Improvements

  • Define supported NodeJS versions, and tweak TypeScript build accordingly.

🔧 Maintenance

  • Dependency Updates

1.22.2

🔧 Maintenance

  • Dependency updates

1.22.1

🐛 Bug Fixes

  • Remove explicit version

1.22.0

💡 Features and Improvements

  • #116 Expand postcss version support allowed with peer

1.21.0

🔧 Maintenance

  • Dependency Updates
  • Documentation Cleanup

1.20.0

🔧 Maintenance

  • Dependency updates

1.19.0

💡 Features and Improvements

  • [109] Fix compatibility with PostCSS 8.1

1.18.1

🐛 Bug Fixes

  • [107] Fix relative path issue Cannot find module 'PostCSSPrefixWrap'

1.18.0

💡 Features and Improvements

  • [104] PostCSS 8 Support

1.17.0

🔧 Maintenance

  • Dependency updates
  • Documentation refactor

1.16.0

🔧 Maintenance

  • Dependency updates

1.15.0

💡 Features and Improvements

  • [91] Clarify prefixRootTags option

1.14.0

💡 Features and Improvements

  • ESLint support to improve code quality

🔧 Maintenance

  • Dependency updates

1.13.2

🔧 Maintenance

  • Dependency updates

1.13.1

🔧 Maintenance

  • Dependency updates

1.13.0

💡 Features and Improvements

  • Implement blacklist

1.12.0

  • [74] Whitelist option for prefixing only some files

1.11.0

💡 Features and Improvements

  • GitHub Actions Release

1.10.0

💡 Features and Improvements

  • GitHub Actions (Replacing TravisCI)

1.9.0

💡 Features and Improvements

  • Add integration tests to validate plugin defined correctly in build.

🐛 Bug Fixes

  • Fix error in main file definition.

1.8.0

💡 Features and Improvements

  • Jest

1.7.0

💡 Features and Improvements

  • Audit Fixes

1.6.0

💡 Features and Improvements

  • Refactored test implementation with acceptance, and unit kinds.
  • TypeScript to better handle types.

1.5.1

🐛 Bug Fixes

  • Updated package metadata.
  • Updated CI configuration to ensure it fails when npm audit fails.

1.5.0

💡 Features and Improvements

  • [41] Added ignored selectors list.
  • Experiment with better eslint / prettier integration.
  • Package updates; some to fix audit errors, the rest to bring packages up to latest versions.

1.4.1

🐛 Bug Fixes

  • Dependency and License updates.

1.4.0

💡 Features and Improvements

  • [28] PostCSS Should be a Peer Dependency not Dependency
  • [29] Use Modern JS (ES6+)
  • [30] Include Current Node Versions in Testing
  • Prettier
  • Project cleanup.

1.3.0

💡 Features and Improvements

  • [22] Implement a more robust @keyframes detection, preserving from and to inside @keyframes

🐛 Bug Fixes

  • [23] Stop ESLint from searching for config files in parent folders

1.2.0

💡 Features and Improvements

  • Add code coverage reporting to ensure all parts of the code are being exercised by the tests
  • Additional test coverage to exercise error condition handling for invalid css
  • Introduction of integration testing suite

🐛 Bug Fixes

  • [17] Keyframe percentages are incorrectly prefixed

1.1.3

🐛 Bug Fixes

  • [6] Selectors containing the word "body" or "html" treated as root

1.1.2

🐛 Bug Fixes

  • Support for multiline selectors

1.1.1

💡 Features and Improvements

  • Updates to support disabling of html/body replacement
  • Enable support for multiple selectors

1.0.1

💡 Features and Improvements

  • Updates to README contents

1.0.0

💡 Features and Improvements

  • Updated npm dependencies to explicitly define versions

0.2.2

💡 Features and Improvements

  • Expanded ESLint coverage to validate test scripts
  • Extracted contributor details into CONTRIBUTING.md file and simplified project README.md

0.2.1

💡 Features and Improvements

  • Added test for our container selector matching existing selector

0.2.0

💡 Features and Improvements

  • Added Mocha Unit tests

0.1.0

💡 Features and Improvements

  • Add support for top level combined selectors containing our container class

0.0.5

🐛 Bug Fixes

  • Updated ignores to help npm

0.0.4

💡 Features and Improvements

  • Initial repository scaffold
  • Basic plugin implementation