从反应索引链接到CSS文件 [英] Linking to css files from react 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>
但是由于某种原因,在运行npm start之后,它无法访问css文件..所以我得到的页面没有样式. 这是我的项目的树:
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:
- 动作
- 组件
- 容器
- dist
- 中间件
- node_modules
- 减速器
- 商店
- ui
- -css
- -字体很棒
- index.html
- index.js
- webpack.config.js
- package.json
- actions
- components
- containers
- dist
- middleware
- node_modules
- reducers
- store
- ui
- --css
- --font-awesome
- index.html
- index.js
- webpack.config.js
- package.json
推荐答案
如果要在HTML中包含CSS,则必须将它们放在 public 文件夹中,并使用'%PUBLIC_URL%'作为CSS的路径.所以看起来像这样:
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" />
或者,强烈建议您确实将想要的css导入您的react组件内,因为您的css会被缩小并捆绑在一起.因此,您可以执行以下操作:
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.
这篇关于从反应索引链接到CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!