Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Media Query not working with CSS/Style loader and Webpack3

I am using css-loader and style-loader for my CSS but all media queries are not working. I am using "webpack": "^3.4.1", "css-loader": "^0.28.4" and "style-loader": "^0.18.2".

This is my Webpack configuration:

const ExtractTextPlugin = require('extract-text-webpack-plugin')

rules: [{
  test: /\.css$/,
  use: [{
    loader: 'style-loader'
  }, {
    loader: 'css-loader',
    options: {
      modules: true,
      localIdentName: '[name]-[local]-[hash:base64:6]',
      camelCase: true
    }
  }]
}]
...
plugins: [
  new ExtractTextPlugin({
    filename: 'style.[chunkhash:6].css',
    allChunks: true
  })
]

My css file is something like this:

.container{
  position: relative;
  margin: 30px 15px;
  padding: 50px 15px;
  background: #fff;
}
@media (max-width: 768px) {
  .container{
    background: #fbfbfb;
  }
}

and I am importing this CSS file in React code like this:

import styles from './Component.css'
like image 247
sahil solanki Avatar asked Aug 01 '17 07:08

sahil solanki


People also ask

Why my media query is not working in CSS?

Media Query Not Working on Mobile Devices If media queries work on desktop and not on mobile devices, then you most likely haven't set the viewport and default zoom. Note: You only need to add one of the code lines above, and usually, the first one does the job.

Do media queries go in CSS?

The CSS Media Query gives you a way to apply CSS only when the browser and device environment matches a rule that you specify, for example "viewport is wider than 480 pixels".

What is CSS loader and style loader?

css-loader is the npm module that would help webpack to collect CSS from all the css files referenced in your application and put it into a string. And then style-loader would take the output string generated by the above css-loader and put it inside the <style> tags in the index.


1 Answers

try use this code

.container{
  position: relative;
  margin: 30px 15px;
  padding: 50px 15px;
  background: #fff;
}
@media only screen and (max-width:768px){
  .container{
    background: #c00;
  }
}
<div class="container">
content her
</div>
like image 174
Abdo-Host Avatar answered Oct 27 '22 00:10

Abdo-Host