0

I am trying to import styles from a .css file into a react js file using "import '../css/banner.css', with css-loader and style-loader installed and enabled. It should be the most direct and simplest method to import css in react, but the styles just won't apply.

I am trying to achieve this without using other libraries like styled-components or jss.


banners.css:

.headerItem{
  width: 20vw;
  float: left;
  background-color: cadetblue;
  margin: auto;
  padding: 3em;
  text-align: center;
}

Header.js

import React ...
import '../css/banners.css'

class Header extends React.Component{
  constructor(){
  }

  render(){
    return (
      <div className={"header"}>
        <HeaderItem/> //shown as an example, has className of "headerItem"
      </div>
    )
  }
}
2
  • your css class is .headerItem and your element has a class of .header ? Commented Mar 12, 2020 at 12:39
  • you should write it as className="headerItem" Commented Mar 12, 2020 at 12:41

4 Answers 4

4

The problem is not relevant to wrapping curly brackets around the class name or not. I found out that in my webpack.config.js I had set the modules option in "css-loader" to be true, which led to the css-loader looking for modules.css files instead of .css files. Changing the modules option to false solved my problem. (If you are using css modules then remember to set the flag to the correct value!)

As a matter of fact, arguments to be passed onto a React component should always be wrapped in curly brackets, and even if you don't the compiler will automatically add them for you since every argument is treated as an object, which would then be collected and passed as props down to the Child Component.

Apologies for raising such a trivial and wrongly-focused question.

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

Comments

1

Try this one. Dont use {} in className

<div className="headerItem">
  <SomeChild/> //shown as an example
</div>

3 Comments

It works now without {}. When do I use and not use the curly braces? I thought in jsx the html properties only accept objects.
Sorry, it wasn't about the curly brackets. See my answer.
With or without curly braces does not affect passing "headerItem" as a prop. JSX allows braces to add functionality for passing expressions including non-literal string values as props. But in here, "headerItem" is a literal string so braces have no difference. Side note: it's common to see braces for className props for dynamically adding classes to components, typically decided by a component state.
1

The best and simplest way to include CS into your react project is;

  1. rename your file to [FileName].module.css
  2. import it into your project using import importedStyles from './[FileName].module.css
  3. use it by calling the imported name . the css style you want to use. eg importedStyles.bodyStyle

rename bannerss.css to banners.module.css:

.headerItem{
  width: 20vw;
  float: left;
  background-color: cadetblue;
  margin: auto;
  padding: 3em;
  text-align: center;
}
Header.js

Call the css file into your project and use;

import React ...
import bannerStyles '../css/banners.module.css'

class Header extends React.Component{
  constructor(){
  }

  render(){
    return (
      <div className={"bannerStyles.header"}>
        <HeaderItem/> //shown as an example, has className of "headerItem"
      </div>
    )
  }
}

This should work fine and its easy.

Reference: https://www.w3schools.com/react/react_css.asp

Let me know if this works!

Comments

0

Dont use curly braces , or best you can use styled components to give css property

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.