【问题标题】:Use CSS in REACT在 REACT 中使用 CSS
【发布时间】:2020-03-01 17:28:35
【问题描述】:

我如何在反应中使用 css。不断抛出 ./src/index.js 未找到模块:无法解析 'c:\Repo\ecom-front\src' 中的 './app.css'。我知道我做错了什么,所以需要一些帮助。 ecom-front/src/core/Menu.js

import React from "react";
import {Link, withRouter} from "react-router-dom";
import styles from "./app.css";

const Menu = () => (
 <div>
  <nav class="navbar">
       <span class="navbar-toggle" id="js-navbar-toggle">
           <i class="fas fa-bars"></i>
       </span>
       <a href="#" class="logo">logo</a>
       <ul class="main-nav" id="js-menu">
           <li>
               <a href="#" class="nav-links">Home</a>
           </li>
           <li>
               <a href="#" class="nav-links">Products</a>
           </li>
           <li>
               <a href="#" class="nav-links">About Us</a>
           </li>
           <li>
               <a href="#" class="nav-links">Contact Us</a>
           </li>
           <li>
               <a href="#" class="nav-links">Blog</a>
           </li>
       </ul>
   </nav>
  </div>
);

export default withRouter(Menu);

ecom-front/public/index.html

<head>
    <link rel="stylesheet" type="text/css" href="app.css">
    // other code......
  </head>

ecom-front/src/user/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Routes from './Routes';
import './app.css';

ecom-front/src/core/app.css

{
            box-sizing: border-box;
            padding: 0;
            margin: 0;
        }

        body {
            font-family: 'Josefin Sans', sans-serif;
        }

        .navbar {
            font-size: 18px;
            background-image: linear-gradient(260deg, #2376ae 0%, #c16ecf 100%);
            border: 1px solid rgba(0, 0, 0, 0.2);
            padding-bottom: 10px;
        }

        .main-nav {
            list-style-type: none;
            display: none;
        }

        .nav-links,
        .logo {
            text-decoration: none;
            color: rgba(255, 255, 255, 0.7);
        }

        .main-nav li {
            text-align: center;
            margin: 15px auto;
        }

        .logo {
            display: inline-block;
            font-size: 22px;
            margin-top: 10px;
            margin-left: 20px;
        }

        .navbar-toggle {
            position: absolute;
            top: 10px;
            right: 20px;
            cursor: pointer;
            color: rgba(255, 255, 255, 0.8);
            font-size: 24px;
        }

        .active {
            display: block;
        }

        @media screen and (min-width: 768px) {
            .navbar {
                display: flex;
                justify-content: space-between;
                padding-bottom: 0;
                height: 70px;
                align-items: center;
            }

            .main-nav {
                display: flex;
                margin-right: 30px;
                flex-direction: row;
                justify-content: flex-end;
            }

            .main-nav li {
                margin: 0;
            }

            .nav-links {
                margin-left: 40px;
            }

            .logo {
                margin-top: 0;
            }

            .navbar-toggle {
                display: none;
            }

            .logo:hover,
            .nav-links:hover {
                color: rgba(255, 255, 255, 1);
            }
        }

所以我尝试了很多不同的方法来放置外部 css 文件,因为我正在尝试使用 flexbox 或不依赖于引导程序。我不知道我是否应该安装一个 npm 包,或者我是否缺少某些东西,因为我只知道实现 css 的传统方法,而不是使用 REACT。

【问题讨论】:

  • 你能分享你的文件夹结构吗?
  • 我添加了照片

标签: html css reactjs flexbox


【解决方案1】:

您导入的 css 文件不正确。

代替

import styles from "./app.css";

这样做

import "./app.css";

没有从 css 文件中导出样式。并使用 className 代替 类

<div className="class">Get work done in over 1450 different categories</div> 

编辑:这是因为您在 ecom-front/src/index.js 中导入 css 的路径是错误的

应该是

import './core/app.css';

【讨论】:

  • @Omar 好的,你能分享一下文件夹结构吗?这样做会产生同样的错误吗?
  • 我添加了上面的照片,但它每次都会给我不同的错误
【解决方案2】:

您无需在该部分中链接它。

另外,使用“className”(区分大小写)代替“class”,如果您想应用“some_class”类,请使用语法

className={ styles.some_class }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-05
    • 2017-08-14
    • 2016-05-31
    • 2015-12-22
    • 2020-10-22
    • 1970-01-01
    • 2018-03-11
    • 2016-12-13
    相关资源
    最近更新 更多