【笔记】React的CSS样式
前言
React的CSS样式学习笔记
定义内联样式
1 | import React from "react"; |
定义外部样式
全局作用域
1 | * { |
1 | import React from "react"; |
局部作用域
1 | * { |
1 | import React from "react"; |
CSS预处理器
通过craco配置CSS预处理
下载依赖
1 | npm install -D @craco/craco |
创建craco配置文件
1 | module.exports = {} |
修改package.json配置文件
1 | { |
Less
下载依赖
1 | npm install -D craco-less |
修改craco配置文件
1 | const CracoLessPlugin = require("craco-less"); |
定义Less文件
1 | * { |
引入Less文件
1 | import React from "react"; |
CSS-in-JS
styled-components
下载依赖
1 | npm install -D styled-components |
定义CSS样式
- 通过标签模板字符串调用函数创建样式组件
- 支持样式的嵌套
1 | import styled from "styled-components"; |
1 | import React from "react"; |
传递参数
1 | import styled from "styled-components"; |
1 | import React from "react"; |
接收参数时定义默认值
1 | import styled from "styled-components"; |
主题样式
- 通过
<ThemeProvider></ThemeProvider>组件包裹根组件,使得根组件内任意子组件的样式组件都可以访问<ThemeProvider></ThemeProvider>组件传入的theme参数
1 | import styled from "styled-components"; |
1 | import React from "react"; |
样式继承
1 | import styled from "styled-components"; |