postcss-sort-style-rules

PostCSS plugin to sort style rules according to selector specificity.

Usage no npm install needed!

<script type="module">
  import postcssSortStyleRules from 'https://cdn.skypack.dev/postcss-sort-style-rules';
</script>

README

PostCSS Sort Style Rules Build Status

PostCSS plugin to sort style rules according to selector specificity.

The order of CSS style rules only matters if two selector share same specificity. If two style rules have different specificity, changing their order will be safe and this will increase the chance that two style rules with same specificity become adjacent style rules. This is where postcss-merge-rules become useful.

If a style rule have a group of selectors, we can only rearrange the order if all selectors in one group have lower specificity than those in the other group.

.post {
  font-size: 1.5rem;
}
#title, div a {
  color: #69c;
}
.post {
  color: #ccc;
}
div {
  box-sizing: border-box;
}
div {
  box-sizing: border-box;
}
.post {
  font-size: 1.5rem;
}
#title, div a {
  color: #69c;
}
.post {
  color: #ccc;
}

Usage

postcss([ require('postcss-sort-style-rules') ])

See PostCSS docs for examples for your environment.