4

I am building a very simple react app. Everything works fine except the CSS file is not loaded, where is the problem?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link type="text/css" rel="stylesheet" href="/animal_fun_facts/src/styles.css" />
  </head>
  <body>
    <div id="root"></div>
    <script src="/animal_fun_facts/src/index.js"></script>
    <script src="/app.compiled.js"></script>
  </body>
</html>

This is the file structure:

enter image description here

3
  • 2
    Does this answer your question? How to import a CSS file in a React Component Commented Jul 29, 2021 at 0:20
  • 2
    Static assets you need to put into public folder, if you are going to reference it that way. Otherwise as the other comment suggested you need to import it into a component. Commented Jul 29, 2021 at 0:20
  • @AbdulrahmanHatem importing resolved the problem, thanks! Commented Jul 29, 2021 at 0:44

2 Answers 2

2

You can use so many ways actually Like css modules,
Since you have created your stylesheet In a separate file, then let's just import it in your application Use the following :

import React from 'react';
import ReactDOM from 'react-dom';
import './animal_fun_facts/src/styles.css';
Sign up to request clarification or add additional context in comments.

Comments

0

Since your CSS is in the same folder as your HTML file, change your CSS href location to:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link type="text/css" rel="stylesheet" href="/styles.css" />
  </head>
  <body>
    <div id="root"></div>
    <script src="/index.js"></script>
    <script src="/app.compiled.js"></script>
  </body>
</html>

(Note: Ideally, all your HTML files should be placed in the public folder)

If you are using Visual Code, as you press the backslash key, a list of files in the current folder will be shown so you can locate your css or js files easily.

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.