【问题标题】:External css is not getting applied in react外部 CSS 未在反应中应用
【发布时间】:2020-11-07 16:02:23
【问题描述】:

我正在为反应代码使用外部样式表。它不工作。我的 VS 反应代码中有五个文件。 index.html, index.js, AppC.js, AppC.css, UserC.js 除了 index.html 所有其他文件都在 src 中。我没有收到任何错误,只有 css 没有被应用。(我没有更改 index.html)(可以将 App.js 文件名更改为 AppC.js 或其他内容吗?我知道我们不能更改索引。 html 和 index.js)

**index.js**

import AppC from './AppC'
import UsersC from './UsersC'
ReactDOM.render(<AppC />,document.getElementById('root')); 

**AppC.js**

import React, { Component } from 'react';
import './AppC.css';
import UsersC from './UsersC';


export default class AppC extends Component {
    render(){
        let style = true;
        return (
            <React.Fragment>
                <h2 className= 'texg'> Hello Css </h2>
                <UsersC rang={style ? 'textg' : 'textb' } />
            </React.Fragment>      
              );
    }
}

**AppC.css**


.txtg{
    color:green;
}
.txtb{
    color:blue;
}

**UserC.js**


import React, {Component} from 'react';

export default class UsersC extends Component{
    render(){
        return (
            <h3 className={this.props.rang}> from heading  </h3>
        );
    }
}

【问题讨论】:

  • 您似乎没有任何适用于您的 DOM 的 CSS 规则。 texg 是唯一的类,它没有 css 规则。
  • 您的班级名称是textg,但在css中您使用的是txtg
  • 哪些 CSS 不适用?绿一或蓝一

标签: css reactjs


【解决方案1】:

texg 更改为txtg

 <React.Fragment>
      <h2 className= 'texg'> Hello Css </h2>
        //To
       <h2 className= 'txtg'> Hello Css </h2>
      <UsersC rang={style ? 'textg' : 'textb' } />
  </React.Fragment>   

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 2014-01-29
    • 1970-01-01
    • 2017-08-24
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    相关资源
    最近更新 更多