
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
postcss-selector-namespace
Advanced tools
A postcss plugin to prefix each rule with a specific selector
$ npm install postcss-selector-namespace
var postcss = require('postcss')
var selectorNamespace = require('postcss-selector-namespace')
var output = postcss()
.use(selectorNamespace({ selfSelector: ':--component', namespace: 'my-component' }))
.process(require('fs').readFileSync('input.css', 'utf8'))
.css
input.css
:--component {
color: black;
}
:--component.danger {
color: red;
}
h1, .h1 {
font-weight: bold;
}
will output the following css:
.my-component {
color: black;
}
.my-component.danger {
color: red;
}
.my-component h1, .my-component .h1 {
font-weight: bold;
}
Prepending :root to a selector prevents the selector from being namespaced:
:root h1 {
font-weight: bold;
}
will output the selector without any namespace:
h1 {
font-weight: bold;
}
This plugin can also process scss files and output scss again using the
postcss-scss module.
var postcss = require('postcss')
var postscss = require('postcss-scss')
var selectorNamespace = require('postcss-selector-namespace')
var output = postcss()
.use(selectorNamespace({ selfSelector: '&', namespace: 'my-component' }))
.process(require('fs').readFileSync('input.css', 'utf8'), { syntax: postscss })
.css
$break = 320px;
& {
float: left;
width: 250px;
h1 {
font-weight: bold;
font-size: 32px;
}
@media screen and (max-width: $break-small) {
width: 100px;
float: none;
h1 {
font-size: 24px;
}
}
}
outputs:
$break = 320px;
.my-component {
float: left;
width: 250px;
h1 {
font-weight: bold;
font-size: 32px;
}
@media screen and (max-width: $break-small) {
width: 100px;
float: none;
h1 {
font-size: 24px;
}
}
}
postcss-importUsing the excellent plugin postcss-import, we can easily namespace each component with its filename.
components/my-button.css
:--namespace {
border: 1px solid #666;
border-radius: 3px;
}
components/my-tabs.css
:--namespace {
display: flex;
}
.tab {
border: 1px solid #666;
border-bottom: none;
border-top-radius: 3px;
margin: 0 5px;
}
main.css
@import 'components/my-button.css';
@import 'components/my-tabs.css';
body {
margin: 0;
color: #333;
}
build.js
const fs = require('fs')
const path = require('path')
const postcss = require('postcss')
const postcssImport = require('postcss-import')
const postcssSelectorNamespace = require('postcss-selector-namespace')
let css = fs.readFileSync('main.css', 'utf8')
function getComponentName(filename) {
if (/components\//.test(filename)) {
return path.basename(filename).replace(/\.css$/, '')
}
return null
}
postcss()
.use(postcssImport({
transform(css, filename, options) {
let componentName = getComponentName(filename)
if (!componentName) {
return css
}
return postcss()
.use(postcssSelectorNamespace({ namespace: '.' + componentName }))
.process(css)
.then(result => result.css)
}
}))
.process(css, { from: 'main.css' })
.then(result => {
console.log(result.css)
})
node build.js outputs:
.my-button {
border: 1px solid #666;
border-radius: 3px;
}
.my-tabs {
display: flex;
}
.my-tabs .tab {
border: 1px solid #666;
border-bottom: none;
border-top-radius: 3px;
margin: 0 5px;
}
body {
margin: 0;
color: #333;
}
namespace(default: '.self')
The selector to prepend to each selector.
selfSelector(default: :--namespace)
The selector to use to apply rules directly to your namespace selector.
ignoreRoot(default: true)
Selector won't be namespaced if they start with the :root pseudo-class.
rootSelector(default: :root)
If prefixed with this selector, selectors won't be namespaced.
dropRoot(default: true)
If true, the rootSelector will be stripped from the output.
FAQs
A postcss plugin to prefix each rule with a specific selector
The npm package postcss-selector-namespace receives a total of 7,448 weekly downloads. As such, postcss-selector-namespace popularity was classified as popular.
We found that postcss-selector-namespace demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.