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

@karlhillx/css-rules-sorter

v1.1.0

Published

PostCSS-based CSS rules sorter that alphabetically sorts selectors and media queries

Readme

CSS Rules Sorter

✨ A PostCSS plugin to automatically sort CSS selectors and media queries for cleaner, more maintainable stylesheets.

npm version MIT License PostCSS 8+ GitHub Actions CI Jest Tests

Key Features

  • Alphabetical Selector Sorting: Automatically sorts top-level selectors and rules within media queries for consistent code.
  • Intelligent Media Query Management: Flexible ordering (mobile-first or desktop-first) and grouping using postcss-sort-media-queries.
  • Advanced Methodologies: Built-in support for BEM-aware sorting.
  • Cascade Layer Support: Automatically reorders @layer blocks to match your defined priority.
  • Modern PostCSS Support: Fully compatible with the latest PostCSS 8+ API.
  • Developer-Focused: Includes TypeScript definitions, comprehensive tests, and linting.

Installation

npm install @karlhillx/css-rules-sorter postcss --save-dev

Usage

Add @karlhillx/css-rules-sorter to your PostCSS configuration (e.g., postcss.config.js):

// postcss.config.js
module.exports = {
  plugins: [
    require('@karlhillx/css-rules-sorter')({
      sort: 'mobile-first', // or 'desktop-first'
      selectorSort: 'natural', // or 'bem' or 'specificity'
      propertyShorthand: 'expand', // or 'collapse'
    }),
  ],
};

Configuration Options

| Option | Type | Default | Description | | ------------------ | --------- | ---------------- | ------------------------------------------------------------------------------ | | sort | string | 'mobile-first' | Sets the media query order: 'mobile-first' or 'desktop-first'. | | selectorSort | string | 'natural' | Defines the method for sorting selectors: 'natural', 'specificity', or 'bem'. | | propertySort | string | 'none' | Sorts properties within each rule: 'none' or 'alphabetical'. | | propertyShorthand| string | 'none' | Manages shorthand properties: 'none', 'expand', or 'collapse'. | | sortLayers | boolean | true | Sorts CSS cascade layers based on the first @layer definition. | | groupByMediaType | boolean | true | Groups media queries by their type (e.g., screen, print). |

Advanced Features

BEM Selector Sorting

Set selectorSort: 'bem' to sort selectors based on the Block, Element, Modifier (BEM) methodology. This ensures your component structures stay logically grouped.

Before:

.card__header--small { font-size: 0.8em; }
.card--featured { border: 1px solid blue; }
.card { color: black; }
.card__header { font-weight: bold; }

After:

.card { color: black; }
.card--featured { border: 1px solid blue; }
.card__header { font-weight: bold; }
.card__header--small { font-size: 0.8em; }

Shorthand Property Management

Use propertyShorthand to enforce a specific style for margin and padding.

  • 'expand': Breaks down shorthands like margin: 10px 20px; into four longhand properties. Excellent for preventing accidental overrides.
  • 'collapse': Combines longhand properties into a single shorthand when all four sides are present.

Cascade Layer Sorting

Set sortLayers: true to automatically reorder your @layer blocks to match the priority defined in your first @layer rule.

Before:

@layer components, base, reset;

@layer base { body { color: red; } }
@layer reset { * { margin: 0; } }
@layer components { .btn { padding: 1em; } }

After:

@layer reset { * { margin: 0; } }
@layer base { body { color: red; } }
@layer components { .btn { padding: 1em; } }

Development

# Install all dependencies
npm install

# Run tests
npm test

# Lint code
npm run lint

# Format code
npm run format

Contributing

  1. Fork the repository.
  2. Create a new feature branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -m 'Add your feature').
  4. Push to the branch (git push origin feature/your-feature).
  5. Create a Pull Request.

License

MIT License. See LICENSE for details.