S
S
SwaG2018-07-04 21:44:45
Layout
SwaG, 2018-07-04 21:44:45

How to combine identical media queries in SASS into one CSS?

There is such a construction in SASS:

.block
  color: red

@media screen and (max-width: 767px)
  .block
    color: green

.block1
  color: blue

@media screen and (max-width: 767px)
  .block1
    color: yellow

Which compiles to CSS like this:
.block {
    color: red
}

@media screen and (max-width:767px) {
    .block {
        color: green
    }
}

.block1 {
    color: blue
}

@media screen and (max-width:767px) {
    .block1 {
        color: yellow
    }
}

How can I make it compile like this?
.block {
    color: red
}

.block1 {
    color: blue
}

@media screen and (max-width:767px) {
  .block {
        color: green
    }
    .block1 {
        color: yellow
    }
}

That is, the question is how to make media queries with the same breakpoints merge into one? Tell me please. I use webpack to build.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dymok, 2018-07-04
@SwaG

https://www.npmjs.com/package/group-css-media-queries

M
max1my5, 2022-04-17
@max1my5

for Webpack:
npm i -D group-css-media-queries-loader

module: {
      rules: [
         {
            test: /\.css$/,
            use: [
               'css-loader',
               'group-css-media-queries-loader'
            ]
         },
         {
            test: /\.s[ac]ss$/,
            use: [
               'css-loader',
               'group-css-media-queries-loader',
               'sass-loader'
            ]
         },
      ]
   },

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question