2

Can't apply css to React className...

import React from "react";
import "./index.css";

class CompanyInfo extends React.Component {

render() {

    return (
        <div className="CompanyInfo">
            <ul>
                <li className="Name">Company Name</li>
                <li className="Production">Production</li>
            </ul>
        </div>
    );
};
}

export default CompanyInfo;

here's my CSS

.CompanyInfo {
list-style: none;
font-family: 'Hind', sans-serif;
color: white;
}

.Name {
font-size: 20px;
}

.Production {
font-size: 16px;
}

but this doesn't work! it still appears like a list

The image to see how it appears

without any CSS :/

Can you help me? :)

AFAF
  • 569
  • 2
  • 16
  • 40

3 Answers3

4

It works now! :)

//Changed this  
import "./index.css" 
//To this
import Styles from "./index.css"


//Change this
<li className="Name">Company Name</li>
//To this
<li className={Styles.Name}>Company Name</li>

Thanks to all of you who helped me in seconds!!!

AFAF
  • 569
  • 2
  • 16
  • 40
3

You have the css style list-style: none; so I think CompanyInfo class should be applied to the ul element instead of the div, it should probably be

return (
    <div>
        <ul className="CompanyInfo">
            <li className="Name">Company Name</li>
            <li className="Production">Production</li>
        </ul>
    </div>
);

or maybe

return (
    <ul className="CompanyInfo">
        <li className="Name">Company Name</li>
        <li className="Production">Production</li>
    </ul>
);
Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56
  • That makes sense, thank you! :) still doesn't work, but that part at least I changed for better – AFAF Sep 10 '18 at 18:54
  • @AnaFig Just to debug.. add body { background: red} in the css file and what do you see now? – Arup Rakshit Sep 10 '18 at 18:56
  • 1
    I suspect your css is not loaded correctly, do you use `webpack` ? Did you declare the `css-loader` and the `style-loader` ? https://stackoverflow.com/questions/39853646/how-to-import-a-css-file-in-a-react-component – Olivier Boissé Sep 10 '18 at 18:57
1

I am surprised you had to make this changes for it to work

//Changed this  
import "./index.css" 
//To this
import Styles from "./index.css"


//Change this
<li className="Name">Company Name</li>
//To this
<li className={Styles.Name}>Company Name</li>

Your first implementation like so

import "./index.css" 


<li className="Name">Company Name</li>

should work well also. I implemented something similar for my own project like so and it works well.

import React from "react";
import "../index.css";

export default function Header() {
  return <header className="navbar">This is my header</header>;
}

my index.css file

.navbar {
  height: 100px;
  background-color: purple;
  color: whitesmoke;
  margin-bottom: 15px;
  text-align: center;
  font-size: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
Manu_Boca
  • 11
  • 1
  • 4