0

Folder Structure How do i use css in react. Keeps throwing ./src/index.js Module not found: Can't resolve './app.css' in 'c:\Repo\ecom-front\src'. I know i'm doing something wrong so would like some help. ecom-front/src/core/Menu.js

import React from "react";
import {Link, withRouter} from "react-router-dom";
import styles from "./app.css";

const Menu = () => (
 <div>
  <nav class="navbar">
       <span class="navbar-toggle" id="js-navbar-toggle">
           <i class="fas fa-bars"></i>
       </span>
       <a href="#" class="logo">logo</a>
       <ul class="main-nav" id="js-menu">
           <li>
               <a href="#" class="nav-links">Home</a>
           </li>
           <li>
               <a href="#" class="nav-links">Products</a>
           </li>
           <li>
               <a href="#" class="nav-links">About Us</a>
           </li>
           <li>
               <a href="#" class="nav-links">Contact Us</a>
           </li>
           <li>
               <a href="#" class="nav-links">Blog</a>
           </li>
       </ul>
   </nav>
  </div>
);

export default withRouter(Menu);

ecom-front/public/index.html

<head>
    <link rel="stylesheet" type="text/css" href="app.css">
    // other code......
  </head>

ecom-front/src/user/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Routes from './Routes';
import './app.css';

ecom-front/src/core/app.css

{
            box-sizing: border-box;
            padding: 0;
            margin: 0;
        }

        body {
            font-family: 'Josefin Sans', sans-serif;
        }

        .navbar {
            font-size: 18px;
            background-image: linear-gradient(260deg, #2376ae 0%, #c16ecf 100%);
            border: 1px solid rgba(0, 0, 0, 0.2);
            padding-bottom: 10px;
        }

        .main-nav {
            list-style-type: none;
            display: none;
        }

        .nav-links,
        .logo {
            text-decoration: none;
            color: rgba(255, 255, 255, 0.7);
        }

        .main-nav li {
            text-align: center;
            margin: 15px auto;
        }

        .logo {
            display: inline-block;
            font-size: 22px;
            margin-top: 10px;
            margin-left: 20px;
        }

        .navbar-toggle {
            position: absolute;
            top: 10px;
            right: 20px;
            cursor: pointer;
            color: rgba(255, 255, 255, 0.8);
            font-size: 24px;
        }

        .active {
            display: block;
        }

        @media screen and (min-width: 768px) {
            .navbar {
                display: flex;
                justify-content: space-between;
                padding-bottom: 0;
                height: 70px;
                align-items: center;
            }

            .main-nav {
                display: flex;
                margin-right: 30px;
                flex-direction: row;
                justify-content: flex-end;
            }

            .main-nav li {
                margin: 0;
            }

            .nav-links {
                margin-left: 40px;
            }

            .logo {
                margin-top: 0;
            }

            .navbar-toggle {
                display: none;
            }

            .logo:hover,
            .nav-links:hover {
                color: rgba(255, 255, 255, 1);
            }
        }

So i tried a lot of different ways to put an external css file, as i am trying to use flexbox or not be dependant on bootstrap. I do not know if i should install an npm package or if i am missing something as i only know the traditional way to implement css just not with REACT.

2
  • Can you share your folder structure? Commented Nov 5, 2019 at 10:48
  • i added the photo Commented Nov 5, 2019 at 11:27

2 Answers 2

2

You are importing the css file incorrectly.

Instead of

import styles from "./app.css";

Do this

import "./app.css";

There is no styles being exported from css file. And use className at place of class

<div className="class">Get work done in over 1450 different categories</div> 

EDIT: It's because the path you are importing the css from in your ecom-front/src/index.js is wrong

It should be

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

2 Comments

@Omar Okay can you share the folder structure ? And does doing this gives the same error?
I added the photo above and it keeps giving me different errors each time
1

You need not link it in the section.

Also, use 'className' (case-sensitive) in place of 'class' and if you want to apply class 'some_class' use the syntax

className={ styles.some_class }

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.