为啥我的 css 不能在 localhost.3000 上应用?
Posted
技术标签:
【中文标题】为啥我的 css 不能在 localhost.3000 上应用?【英文标题】:Why can't my css apply on localhost.3000?为什么我的 css 不能在 localhost.3000 上应用? 【发布时间】:2021-09-05 05:34:01 【问题描述】:我的 INDEX.CSS 无法在我的 LOCALHOST.300 上应用。我试图安装 CSS 加载器和东西,但它仍然无法正常工作,任何人都可以帮助我吗?! 这是我的 Header.js: 从“反应”导入反应 从 'prop-types' 导入 PropTypes; 常量头 = (title) => 返回 (
<h1>title</h1>
</header>
)
Header.defaultProps =
title:'Task Tracker '
Header.propTypes=
title:PropTypes.string.isRequired,
// css in js
// const headingStyle =
// color:'red',
// backgroundColor:'blue'
//
export default Header
这是我的 App.js:
import React from 'react';
import "./index.css"
import Header from "./components/Header";
function App()
return (
<div className="Container">
<Header />
</div>
)
export default App
这是我的 index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as reportWebVitals from './reportWebVitals';
require('./index.css');
ReactDOM.render(<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root'));
这是我的 index.css:
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400&display=swap');
*
box-sizing: border-box;
margin: 0;
padding: 0;
body
font-family: 'Poppins', sans-serif;
.container
max-width: 500px;
margin: 30px auto;
overflow: auto;
min-height: 300px;
border: 1px solid steelblue;
padding: 30px;
border-radius: 5px;
.header
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
.btn
display: inline-block;
background: #000;
color: #fff;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
font-size: 15px;
font-family: inherit;
.btn:focus
outline: none;
.btn:active
transform: scale(0.98);
.btn-block
display: block;
width: 100%;
.task
background: #f4f4f4;
margin: 5px;
padding: 10px 20px;
cursor: pointer;
.task.reminder
border-left: 5px solid green;
.task h3
display: flex;
align-items: center;
justify-content: space-between;
.add-form
margin-bottom: 40px;
.form-control
margin: 20px 0;
.form-control label
display: block;
.form-control input
width: 100%;
height: 40px;
margin: 5px;
padding: 3px 7px;
font-size: 17px;
.form-control-check
display: flex;
align-items: center;
justify-content: space-between;
.form-control-check label
flex: 1;
.form-control-check input
flex: 2;
height: 20px;
footer
margin-top: 30px;
text-align: center;
我的 4 个文件(index.css、App.js、Header.js 和 index.js)位于我的组件文件夹中,该文件夹位于我的 src 文件夹中
这是 package.json:
"name": "react-task-number",
"version": "0.1.0",
"private": true,
"dependencies":
"@testing-library/jest-dom": "^5.13.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-build-dist": "^0.0.8",
"react-dom": "^17.0.2",
"react-scripts": "^1.1.5",
"web-vitals": "^1.1.2"
,
"scripts":
"build:dist": "react-build-dist",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
,
"eslintConfig":
"extends": [
"react-app",
"react-app/jest"
]
,
"browserslist":
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
,
"devDependencies":
"css-loader": "^5.2.6",
"style-loader": "^0.19.0"
这是 webpack.config.js:
const path = require('path');
module.exports =
entry: './src/app.js',
output:
filename: 'App.js',
path: path.resolve(__dirname, 'dist')
,
module:
rules: [
test: /\.js$/,
exclude: /node_modules/,
use:
loader: 'babel-loader',
options:
presets: ['@babel/preset-env']
,
// Relevant bit of config for style loader and css loader:
test: /\.css$/,
// the order of `use` is important!
use: [ loader: 'style-loader' , loader: 'css-loader' ],
,
]
,
;
这是 webpack.config.prod.js:
const path = require('path');
const merge = require('webpack-merge');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const common = require('./webpack.common.js');
module.exports = merge(common,
entry: 'apps/index.js',
mode: 'production',
devtool: 'source-map',
output:
path: path.resolve(__dirname, 'dist/'),
publicPath: '/dist/',
filename: 'bundle.js',
libraryTarget: 'commonjs2',
,
plugins: [
new CleanWebpackPlugin(['dist/*.*']),
],
externals:
react: 'react',
'react-dom': 'react-dom',
,
);
【问题讨论】:
请修正您的代码格式,不要对我们大喊大叫。 【参考方案1】:因为您在 JSX 中将“Container”大写,而您的 css 中的类名是小写的
【讨论】:
你能说得更具体些吗? JSX 在哪里?对不起,我只是个初学者 哦,它有效,谢谢你!上帝保佑你! 你应该将我的答案标记为正确然后 =] 很高兴我能提供帮助以上是关于为啥我的 css 不能在 localhost.3000 上应用?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 CSS 在使用 xampp 的 localhost 中不能正常工作,但在本地或从 Netlify 站点打开相同的文件时工作正常?
为啥 webpack 不能导入 PrimeReact CSS 文件?
为啥 CSS 中的 background:url 不能与 Django 一起使用?