2

I'm using create-react-app and have read bad things about inline styles so I wanted to use css modules however they aren't supported by create-react-app at this time. Can I literally just use plain old css in one big file? Also with this approach how do I style a react component. For example I have a component and I give it a class name: <card className="cardStyle" />. Why does this not work? I want to bed able to position it just like I would a div.

1
  • yes, you can. React just generates HTML, it does not change anything about the way CSS work. Commented Jun 22, 2017 at 8:34

3 Answers 3

1

it's certainly possible, you may have plain old CSS classes in a style.css somewhere, but you'll have to make sure your app includes it,

e.g. have in your App.js

import './style.css'
Sign up to request clarification or add additional context in comments.

Comments

1

The standard way is to do the following;

import React, { Component } from 'react';
import './Button.css'; // Tell Webpack that Button.js uses these styles

class Button extends Component {
  render() {
    // You can use them as regular CSS styles
    return <div className="Button" />;
  }
}

See: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-a-stylesheet

Comments

0
  1. Using CSS Modules might be better way:

CSS Modules have .module.css in their file name, like styles.module.css. Naming it so is important it avoids serious issue in plain CSS when having same CSS class names in different CSS files.


File : styles.module.css

.myClass1 {
    background-color: red;
}

File : App.js

import styles from'./styles.module.css';

<div   className={styles['myClass1']}  >
   some content
</div>

By default supported by Create React App - https://reactjs.org/docs/create-a-new-react-app.html
This can done using simple steps

npx create-react-app my-app
cd my-app
npm start

Links: . https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/
. https://www.freecodecamp.org/news/how-to-style-react-apps-with-css/


  1. If you want to use normal css but want more flexibilities like props to css, and open to install more dependencies then alternatives are:

. https://emotion.sh/docs/introduction
. https://styled-components.com/

Some are migrating styled-components to emotion, so may be emotion is better way to start with. See https://storybook.js.org/blog/541-components-from-styled-components-to-emotion/


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.