3

i have a trouble importing .css stylesheet on a react basic app, I have this components:

AvatarHeader.js:

import styles from './AvatarHeader.css';

export default function AvatarHeader({ style, children }) {
  return (
    <div>
      <img style={styles.image} src={Background} alt="background">
        { children }
      </img>

AvatarHeader.css:

.image {
   width: 400
}

AvatarHeader.js & AvatarHeader.css are in the same folder.

package.json:

  {
    "name": "UtWeb",
    "version": "0.1.0",
    "private": true,
    "devDependencies": {
      "css-loader": "^0.25.0",
      "react-scripts": "0.9.0",
      "webpack": "^2.2.1",
      "webpack-dev-server": "^1.9.0"
    },
    "dependencies": {
      "react": "^15.4.2",
      "react-dom": "^15.4.2",
      "react-redux": "^4.3.0",
      "react-router": "^2.0.0",
      "redux": "^3.2.1",
      "react-router-redux": "^4.0.0"
    },
    "scripts": {
      "start": "react-scripts start",
      "build": "react-scripts build",
      "test": "react-scripts test --env=jsdom",
      "eject": "react-scripts eject"
    }
  }

Styles aren't applied on the component, but it's works if I rewrite AvatarHeader.js so:

  import styles from './AvatarHeader.css';

  const image = {
    width: 400
  }

  export default function AvatarHeader({ style, children }) {
    return (
      <div>
        <img style={image} src={Background} alt="background">
          { children }
        </img>

I don't understand how to fix this bug for use the first way to import css file.

2
  • You don't import the CSS file and use it like an object. You just have to import the file with no name just for side effects and then give the .image class to the img. Commented Mar 1, 2017 at 22:27
  • 2
    You may try to use css-object-loader instead of css-loader, since it will give you an object instead of loading the CSS globally (which is what css-loader does). Commented Mar 1, 2017 at 22:31

3 Answers 3

9
import './AvatarHeader.css';`
<img className='image' />` 

is the correct syntax

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

3 Comments

Perfect! This works, but another question what pass if I have another imported css file with a 'image' class. How can i define where to use the class?
If you're using webpack and css-loader, you can define scope. More info here: github.com/webpack-contrib/css-loader#css-scope
this import css globally? or just for the component ?
2

In case anyone runs into this problem again, the stylesheet needs to follow the module naming convention for the OP's original syntax to work.

So once the file is renamed AvatarHeader.module.css, the following React code should work:

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

export default function AvatarHeader({ style, children }) {
  return (
    <div>
      <img style={styles.image} src={Background} alt="background">
        { children }
      </img>

Comments

2

You need to change the CSS file name from AvatarHeader.css to AvatoarHeader.module.css. then the import statement should be: import styles from './AvatarHeader.module.css'. It should work now. see docs

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.