Adding a Stylesheet

This project setup uses Webpack for handling all assets. Webpack offers a custom way of “extending” the concept of import beyond JavaScript. To express that a JavaScript file depends on a CSS file, you need to import the CSS from the JavaScript file:

Button.css

  1. .Button {
  2. padding: 20px;
  3. }

Button.js

  1. import React, { Component } from 'react';
  2. import './Button.css'; // Tell Webpack that Button.js uses these styles
  3. class Button extends Component {
  4. render() {
  5. // You can use them as regular CSS styles
  6. return <div className="Button" />;
  7. }
  8. }

This is not required for React but many people find this feature convenient. You can read about the benefits of this approach here. However you should be aware that this makes your code less portable to other build tools and environments than Webpack.

In development, expressing dependencies this way allows your styles to be reloaded on the fly as you edit them. In production, all CSS files will be concatenated into a single minified .css file in the build output.

If you are concerned about using Webpack-specific semantics, you can put all your CSS right into src/index.css. It would still be imported from src/index.js, but you could always remove that import if you later migrate to a different build tool.