Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to css files from react index

I have this simple index :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>SB Admin - Bootstrap Admin Template</title>

<!-- Auth0Lock script -->
<script src="//cdn.auth0.com/js/lock-9.1.min.js"></script>

<!-- Bootstrap Core CSS -->
<link href="/ui/css/bootstrap.min.css" rel="stylesheet" />

<!-- Custom CSS -->
<link href="/ui/css/sb-admin.css" rel="stylesheet" />

<!-- Custom Fonts -->
<link href="/ui/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

 </head>
  <body>
    <div id="root">
    </div>
    <script src="/static/bundle.js"></script>
  </body>
</html>

But for some reason, after running npm start, it doesn't get access to the css files.. so i get a page without style. Here is the tree of my project:

  • actions
  • components
  • containers
  • dist
  • middleware
  • node_modules
  • reducers
  • store
  • ui
  • --css
  • --font-awesome
  • index.html
  • index.js
  • webpack.config.js
  • package.json
like image 337
Remis07 Avatar asked Jun 22 '17 14:06

Remis07


1 Answers

If you want to include css in your html, you gotta put them in your public folder and use '%PUBLIC_URL%' as the path to your css. So it would look something like this:

<link href="%PUBLIC_URL%/bootstrap.min.css" rel="stylesheet" />

Alternatively, it is highly recommended that you do import the css that you want inside your react component, because your css would get minified and bundled. So you could do something like:

import React, { Component } from 'react';
// ... rest of imports
import './path/to/css/font-awesome.min.css'

For more info on this, go to the documentation here.

like image 163
Mμ. Avatar answered Oct 16 '22 08:10

Mμ.