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 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-safe-important

v2.0.1

Published

PostCSS plugin that adds !important to style declarations safely

Downloads

23,482

Readme

PostCSS Safe Important

Build Status Npm Downloads Total Node Version Npm

PostCSS plugin that adds !important to style declarations safely.

Why would I need it?

You should probably look at shadow dom and web components first.

Quoted from Cleanslate

When there are existing CSS styles on a page, and you want to prevent those styles cascading into some part of the page. This is not a stylesheet to use when developing your own website (for that, try Eric Meyer’s classic “Reset CSS” or the “HTML5 Doctors’ adaptation”.

The stylesheet can be useful when distributing content (e.g. a widget, or syndicated news) to third-party websites. The CSS rules in the host site may be unknown and unpredictable, or may change in future without notice, or there may be so many websites you need to distribute to that it is impractical to write specific CSS that overrides the styles in each one. In such situations, the Cleanslate stylesheet will aggressively reset your portion of the content (and nothing else) back to some reasonable default values that you can then build from.

Why not just use an iframe?

Third-party content is often distributed in iframes. Because JavaScript within an iframe can be prevented from accessing the host page, iframes are particularly useful when the host site has security concerns and does not explicitly trust the third-party content.

However, iframes have some drawbacks:

  • You cannot display content outside of the box of the iframe.
  • It is tricky to resize the iframe to match the size of its contents.
  • Your content will be unable to interact with the host page, even if it is trusted.
  • Search engines like Google will not see the content on the host page. Content that is syndicated from a partner website can avoid this by being directly included in the host page.

Whether you work with extreme CSS reset stylesheet like Cleanslate or simply just want to give maximum weight for all your declarations, don't do it manually! Use postcss-safe-important to keep your source styles clean and portable.

Safe?

Adding !important to every declarations might break your style. For example, declarations in a keyframe that are qualified with !important are ignored.

postcss-safe-important will skip unnecessary declarations. You can also set your own exclusions through options or comments (see examples below).

/* Input example */
.foo {
    width: 100px;
    color: #000; /* no !important */ }

.bar {
    /* no important */
    color: ##fff;
    width: 100px; }
/* Output example */
.foo {
    width: 100px !important;
    color: #000; }

.bar {
    color: ##fff;
    width: 100px; }

Usage

$ npm install postcss-safe-important --save-dev

Example

var safeImportant = require("postcss-safe-important");

postcss([
    safeImportant({
        // options
        excludeSelectors: "#bar", // config with string
        excludeDeclarations: /color/, // config with regex
        excludeCSSVariables: ["--width", "--height"], // config with array of string
        excludeAtRules: (atRule) => atRule === "media", // config with function
        excludePaths: p => p.startsWith(path.resolve(__dirname, "../node_modules")), // exclude paths
        disableDefaultExcludes: false, // disable default exclusion lists
        keepComments: true, // all the `no important` comments will be erased
    }),
]);

See tests for more examples.

Comments

You can use either /* no !important */ or /* no important */ to indicate no changing.

If the comment is right inside a rule(be the first child node of the rule), the whole rule will not change.

/* Input example */
.foo {
    /* no important */
    width: 100px;
    color: #000; }

.bar { /* no !important */
    width: 100px;
    color: #000; }
/* Output example */
.foo {
    width: 100px;
    color: #000; }

.bar {
    width: 100px;
    color: #000; }

If the comment is right behind(or below) a declaration, then only the declaration will remain the same.

/* Input example */
.foo {
    width: 100px;
    color: #000; /* no important */}
/* Output example */
.foo {
    width: 100px !important;
    color: #000; }

Options

Exclusions

  • excludeSelectors: exclude selectors. Default empty (default exclusions still applies unless options.disableDefaultExcludes = true)
  • excludeDeclarations: exclude declarations. Default empty (default exclusions still applies unless options.disableDefaultExcludes = true)
  • excludeAtRules: exclude atrules(e.g. @font-face). Default empty (default exclusions still applies unless options.disableDefaultExcludes = true)
  • excludeCSSVariables: exclude CSS variables. Default excludes all CSS Variables.
  • excludePaths. exclude style paths. Default empty.

You can pass either a string, a regexp, an iterable, or a shouldExclude(rule: string): boolean function.

var safeImportant = require("postcss-safe-important");

postcss([
    safeImportant({
        // options
        excludeSelectors: "#bar", // config with string
        excludeDeclarations: /color/, // config with regex
        excludeCSSVariables: ["--width", "--height"], // config with array of string
        excludeAtRules: (atRule) => atRule === "media", // config with function
        excludePaths: p => p.startsWith(path.resolve(__dirname, "../node_modules")), // exclude paths
    }),
]);

If you want styles in node_modules left untouched, let's say your postcss config file is at project root, you can:

var safeImportant = require("postcss-safe-important");
var path = require("path");

postcss([
    safeImportant({
        excludePaths: p => p.startsWith(path.resolve(__dirname, "./node_modules")),
    }),
]);

Keep /* no important */ comments

  • keepComments: bool, default false.

Disable Default Declarations

  • disableDefaultExcludes: bool, default false.

Disable the default exclusion list below.

Default Exclusions

Variables

All CSS variables.

Atrules

  • keyframes
  • font-face

Declarations

  • animation
  • animation-name
  • animation-duration
  • animation-timing-function
  • animation-delay
  • animation-iteration-count
  • animation-direction
  • animation-fill-mode
  • animation-play-state

Change Log

See PostCSS docs for examples for your environment.

License

MIT