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

Package detail

postcss-sort-media-queries

yunusga1.7mMIT5.2.0

PostCSS plugin for sorting and combining CSS media queries with mobile first / **desktop first methodologies

postcss, postcss-plugin, css, css-optimizations, sort, mobile-first, desktop-first, mediaquery, media-queries, mq, responsive-css, combine-media-query, sort-media-query, css-mqpacker, node-css-mqpacker

readme

PostCSS Sort Media Queries

npm Node.js CI license npm

🌏 EnglishO'zbek

PostCSS plugin for sorting and combining CSS media queries with mobile first / desktop first methodologies.

From 5.0.0 plugin support Media Feature Types: “range”

Table of Contents

Online demo

And here is the online demo

Examples

Mobile first sorting

before

@media screen and (max-width: 640px) {
  .head { color: #cfcfcf }
}
@media screen and (max-width: 768px) {
  .footer { color: #cfcfcf }
}
@media screen and (max-width: 640px) {
  .main { color: #cfcfcf }
}
@media screen and (min-width: 1280px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (width > 640px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (max-width: 640px) {
  .footer { color: #cfcfcf }
}

after

@media screen and (width > 640px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (min-width: 1280px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (max-width: 768px) {
  .footer { color: #cfcfcf }
}
@media screen and (max-width: 640px) {
  /* combined */
  .head { color: #cfcfcf }
  .main { color: #cfcfcf }
  .footer { color: #cfcfcf }
}

Desktop first sorting

before

@media screen and (width < 640px) {
  .header { color: #cdcdcd }
}
@media screen and (min-width: 760px) {
  .desktop-first { color: #cdcdcd }
}
@media screen and (width < 640px) {
  .main { color: #cdcdcd }
}
@media screen and (min-width: 1280px) {
  .desktop-first { color: #cdcdcd }
}
@media screen and (max-width: 760px) {
  .footer { color: #cdcdcd }
}
@media screen and (max-width: 640px) {
  .footer { color: #cdcdcd }
}

after

@media screen and (max-width: 760px) {
  .footer { color: #cdcdcd }
}
@media screen and (width < 640px) {
  /* combined */
  .header { color: #cdcdcd }
  .main { color: #cdcdcd }
  .footer { color: #cdcdcd }
}
@media screen and (min-width: 760px) {
  .desktop-first { color: #cdcdcd }
}
@media screen and (min-width: 1280px) {
  .desktop-first { color: #cdcdcd }
}

Install

First thing's, install the module:

npm install postcss postcss-sort-media-queries --save-dev

Usage

Check you project for existed PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you already use PostCSS, add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('postcss-sort-media-queries')({
+     sort: 'mobile-first', // default value
+   }),
    require('autoprefixer')
  ]
}

or with custom sort function

module.exports = {
  plugins: [
+   require('postcss-sort-media-queries')({
+     sort: function(a, b) {
+        // custom sorting function
+     }
+   }),
    require('autoprefixer')
  ]
}

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Options

Sorting works based on dutchenkoOleg/sort-css-media-queries function.

sort

This option support string or function values.

  • {string} 'mobile-first' - (default) mobile first sorting
  • {string} 'desktop-first' - desktop first sorting
  • {function} your own sorting function

'mobile-first'

postcss([
  sortMediaQueries({
    sort: 'mobile-first' // default
  })
]).process(css);

'desktop-first'

postcss([
  sortMediaQueries({
    sort: 'desktop-first'
  })
]).process(css);

Custom sort function

postcss([
  sortMediaQueries({
    function(a, b) {
      return a.localeCompare(b);
    }
  })
]).process(css);

In this example, all your media queries will sort by A-Z order.

This sorting function is directly passed to Array#sort() method of an array of all your media queries.

Sort configuration

By this configuration you can control sorting behaviour.

postcss([
  sortMediaQueries({
    configuration: {
      unitlessMqAlwaysFirst: true, // or false
    }
  })
]).process(css);

Or alternatively create a sort-css-mq.config.json file in the root of your project. Or add property sortCssMQ: {} in your package.json.

Only Top Level

Sort only top level media queries to prevent eject nested media queries from parent node

postcss([
  sortMediaQueries({
    onlyTopLevel: true,
  })
]).process(css);

Changelog

See Releases history

License

MIT

Other PostCSS plugins

  • postcss-momentum-scrolling - plugin for adding momentum style scrolling behavior (-webkit-overflow-scrolling:touch) for elements with overflow (scroll, auto) on iOS

Thanks

changelog

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[5.2.0] 2023-05-30

  • Add onlyTopLevel option prevent eject nested media queries from parent

[5.1.0] 2023-04-25

  • Added tests for CSS Level 4 range type media query
  • Fix broken eslint tests
  • Bump yaml and lint-staged

[5.0.0] 2023-04-23

[4.4.0] 2023-04-22

[4.3.0] 2022-10-06

  • Update postcss to 8.4.16
  • Update sort-css-media-queries to v2.1.0
    • support "min-width: 0" expression on mobile-first sort

[4.2.1] 2021-11-28

  • Update postcss to 8.4.4
  • Update dev dependencies

[4.1.0] 2021-09-13

  • Add module for browser

[4.0.0] 2021-09-13

[3.12.13] 2021-09-12

  • Update postcss to 8.3.6
    • Fixed column in missed semicolon error (by @Gusted).
  • Update sort-css-media-queries to v2.0.4
    • Fix package dependencies
  • Update dev dependencies
  • Fixed dependencies vulnerabilities
  • Add online demo

[3.11.12] 2021-06-10

  • Update postcss to postcss/releases/tag/8.3.1
    • Fixed false positives PostCSS does nothing warning on syntax option.
  • Update dev dependencies
  • Fixed dependencies vulnerabilities

[3.10.11] 2021-05-18

[3.9.10] 2021-05-03

  • Update postcss to postcss/releases/tag/8.2.13
    • Fixed ReDoS vulnerabilities in source map parsing
  • Update dev dependencies
  • Added tests with media-minmax and flexbugs-fixes

[3.8.9] 2021-04-12

  • Update postcss to postcss/releases/tag/8.2.10
    • Fixed ReDoS vulnerabilities in source map parsing
    • Fixed webpack 5 support
    • Fixed docs
  • Update dev dependencies

[3.7.7] 2021-02-11

[3.6.5] 2021-03-05

[3.4.3] 2021-02-11

[3.3.3] 2021-02-07

[3.2.3] 2021-01-19

[3.1.1] 2021-01-08

[3.0.0] 2021-01-03

  • Fix #31 plugin was triggered earlier than postcss-nested

[2.4.17] 2020-12-31

[2.3.17] 2020-12-29

  • Update dev dependencies
  • Bump node-notifier from 8.0.0 to 8.0.1 #29 fixes a security vulnerability

[2.3.15] 2020-12-09

[2.2.14] 2020-12-04

[2.2.13] 2020-12-03

  • Update postcss 8.1.10 -> 8.1.13

[2.2.12] 2020-12-01

  • Update postcss 8.1.6 -> 8.1.10
  • Update dev dependencies

[2.1.11] 2020-11-08

  • Update postcss 8.1.2 -> 8.1.6
  • Update dev dependencies

[2.1.10] 2020-10-19

  • Update postcss 8.1.0 -> 8.1.2
  • Update dev dependencies

[2.1.9] - 2020-09-27

  • Remove yarn.lock
  • Forget say thanks for Andrey Sitnik!
  • Fix Travis-CI build config

[2.1.6] - 2020-09-27

  • Remove .gitattributes and CHANGELOG.md from npm package

[2.1.5] - 2020-09-27

  • PostCSS 8.1 compatability
  • Fixed line endings for test files

[2.0.3] - 2020-09-24

  • Remove coverage directory from npm package
  • Forget say thanks for Jakub Caban!
  • Removed clean-publish dependecie security reason
  • Update yarn.lock

[2.0.0] - 2020-09-24

  • Migrate to PostCSS 8

[1.7.26] - 2020-09-12

  • Update dependencies

[1.7.25] - 2020-07-17

  • Update dependencies

[1.7.24] - 2020-07-07

  • Update dependencies

[1.6.24] - 2020-06-06

  • Update dependencies

[1.5.24] - 2020-05-22

  • Update dev dependencies

[1.4.24] - 2020-04-15

  • Update dev dependencies

[1.4.23] - 2020-03-24

  • Fix check sort property #20
  • Update dev dependencies

[1.3.22] - 2020-03-15

  • Update dev dependencies

[1.3.21] - 2020-02-19

  • Update dependencies
  • Add yarn.lock

[1.1.20] - 2020-02-05

  • travis ci strange cache old files

[1.1.19] - 2020-02-04

  • No reasons for update, just informations fixes

[1.1.18] - 2020-02-03

  • index.test.js eslint fix
  • Update dev dependencies

[1.1.16] - 2020-01-28

Bumps

  • postcss ^7.0.23^7.0.26
  • @logux/eslint-config ^35.0.0^35.0.3
  • eslint ^6.7.0^6.8.0
  • eslint-plugin-import ^2.18.2^2.20.0
  • eslint-plugin-jest ^23.0.4^23.6.0
  • eslint-plugin-node ^10.0.0^11.0.0
  • eslint-plugin-unicorn ^13.0.0^15.0.1
  • jest ^24.9.0^25.1.0

[1.0.15] - 2019-12-12

Added

  • test/, index.test.js to .npmignore to reduce package size

Bumps

  • eslint-plugin-import from 2.19.0 to 2.19.1

[1.0.13] - 2019-12-09

Bumps

  • postcss from 7.0.23 to 7.0.24
  • eslint-plugin-import from 2.18.2 to 2.19.0
  • eslint from 6.7.1 to 6.7.2
  • eslint-plugin-jest from 23.0.5 to 23.1.1

[1.0.9] - 2019-10-29

Bumps

  • @logux/eslint-config from 34.0.1 to 35.0.0
  • sort-css-media-queries from 1.4.3 to 1.5.0

[1.0.7] - 2019-10-25

Bumps

  • eslint from 6.7.0 to 6.7.1
  • eslint-plugin-jest from 23.0.4 to 23.0.5

[1.0.5] - 2019-10-25

1.0.5

  • Update repository, bugs, homepage urls in package.json

1.0.4

  • Add linux and osx os for Travis CI
  • Add my other PostCSS plugins to README.md

1.0.2

  • Fix badges in README.md

1.0.1

  • Update README.md