Heim  >  Fragen und Antworten  >  Hauptteil

Meine App wendet kopiertes CSS nicht auf index.css in React an

Also ich folge einem Tutorial auf Youtube zu ReactJS. Der Youtuber hat mir gesagt, ich solle die CSS-Datei in meine index.css importieren, und dann sollte die Darstellung der Website korrekt angezeigt werden, wenn ich jedoch das CSS kopiere und in meine index.css importiere App.js und nichts taucht in Index.js auf. Weiß jemand, wie man damit umgeht? Hat die React-Version einen anderen Stil? Gibt es noch andere Einstellungen, die mir fehlen?

Hier ist 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;
}

Das sind meine App.js und Index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';
import './index.css';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    <App />
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint.
reportWebVitals();

Endlich App.js:

import Header from "./components/Header";
import Tasks from "./components/Tasks";
import './index.css';


function App() {
  return (
    <div className="Container">
      <Header title='Orpheus Research'/>
      <Tasks />
    </div>
  );
}

export default App

P粉895187266P粉895187266184 Tage vor399

Antworte allen(1)Ich werde antworten

  • P粉930534280

    P粉9305342802024-03-31 00:32:36

    index.css 应导入到index.js 文件中。所以你可以直接删除app.js中的导入。

    由于您使用“./index.css”作为路径导入,因此您需要确保index.css文件与index.js位于同一文件夹中(我认为它是src文件夹)。

    Antwort
    0
  • StornierenAntwort