0

I have been trying to use external css on my React project. I am using webpack with babel. I have configured css, js and jsx loaders on my project. As a matter of fact my project is able to compiled successfully yet I couldn't able to style applied on my html.

Here is my style-header.css file:

.LogoMargin {
  margin-top: 10px;
  margin-left: 10px;
}

Here is my jsx file in which I want to use css style

import React,{Component} from 'react';
import { withStyles } from 'material-ui/styles';
import TextField from 'material-ui/TextField';
import logo from '../resources/images/logo.png';
import '../resources/style/style-header.css';

class Header extends Component {
  render () {
    return(
      <div>
        <span>
          <img className="LogoMargin" src={logo} height="60" width="200" alt="logo" />
        </span>
        <TextField
          id="search"
          label="Search"
          type="search"
          />
      </div>
    )
  }
}

export default Header;

Here webpack config file:

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  devtool: 'eval-source-map',
  entry:  __dirname + "/app/index.js",
  output: {
    path: __dirname + "/build",
    filename: "bundle.js"
  },

  module: {
    loaders: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.css$/,
        exclude: /node_modules/,
        loader: 'css-loader'
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [
          'file-loader',
          {
            loader: 'image-webpack-loader',
            options: {
              bypassOnDebug: true,
            },
          },
        ]
      }
    ]
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: __dirname + "/index.tmpl.html"
    }),
    new webpack.HotModuleReplacementPlugin()
  ],

  devServer: {
    historyApiFallback: true,
    inline: true,
    stats: {colors: true},
    hot: true
  }
}
2
  • Provide webpack config Commented Nov 25, 2017 at 11:22
  • Just updated the post Commented Nov 25, 2017 at 11:31

3 Answers 3

1

Add style loader for handling css as well.

Because you didn't provide webpack version, I cannot provide any code, but I'm sure you will figure out how to use it.

https://github.com/webpack-contrib/style-loader

Sign up to request clarification or add additional context in comments.

Comments

0

You should add "style-loader" for your webpack configuration. You can provide it in array as the following. Also, I have an example in this repo:

https://github.com/burakhanalkan/simple-react-materialui-app-starter/blob/master/webpack.config.js

{
   test: /\.css$/,
   use: [
        "style-loader",
        "css-loader"
   ]
 },

Comments

0

Well I was able to solve this problem as proposed by answers to use style-loader and also I made some changes to my webpack config file which are as follow:

test: /\.css$/,
exclude: /node_modules/,
use: [
  { loader: "style-loader" },
  { loader: "css-loader" ,
    query: {
      modules: true,
      localIdentName: '[name]__[local]___[hash:base64:5]'
    }}
]

But I wonder when I set up react environment using Create React App I was able to use styles without using css module loader.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.