reactjs - 在 react 中为特定组件添加 style.css
问题描述
我正在尝试为 react 中的特定组件添加 CSS 文件,但 CSS 文件适用于所有组件如何为特定组件添加 style.css?
import React, { Component } from "react";
import { Link } from "gatsby";
import Layout from "../components/layout"
import Footer from "../components/Globals/Footer"
import "./crm2.css"
class Crm extends Component {
render() {
...
}
}
解决方案
Your CSS className
must be unique. If you write CSS for this class then it will be applied to the specified component.
There are two components in React
- Built-in component (like
<p>
,<div>
,<span>
, etc) - React component (like
<App/>
,<Product>
, etc)
React components allows you to break up the UI into different pieces so that it can then be reused and handled individually. It is the Dot-notation component like <UserContext.Provider>
and any component which starts with a capital letter.
These components can be styled if you provide a unique className
to those components and you write a CSS style for that.
You might have styled the built-in component as shown below
style.css file:
p {
font-size:14px;
color: red;
}
Using the above approach, the CSS would be applied to all the <p>
component in all the JSX file.
If you have wanted to style the React component then you need to select those components and styled it as shown below.
<Product className = "items" />
CSS would be
.items {
color: red;
...
}
推荐阅读
- java - 静态和非静态方法的同步
- laravel - Composer 更新挂起,但 composer install 正在运行
- mongodb - 如何根据mongodb中的2个字段计算时间差
- maven - 从 AEM 6.4 中删除项目
- java - 使用 Java-8 时 varargs 中的 ClassCastException
- amazon-web-services - AWS X-Ray 在 Edge Lambda 和 Authorizer lambda 中不起作用
- jenkins-pipeline - Jenkins 管道 npm install EACSS 权限被拒绝
- python - 如何使用 python 列表有效地重命名数据框索引?
- javascript - Django 抛出 HTTP 状态码 405 Method not allowed 错误
- r - 将数据框中的每一行与另一个数据框中的多行进行比较并获得结果