網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
React引入css的方法
<div style="color:red">測(cè)試數(shù)據(jù)</div> //報(bào)錯(cuò)
在React中,如果直接按上面的方式寫內(nèi)聯(lián)樣式會(huì)直接報(bào)錯(cuò),因?yàn)镴SX語(yǔ)法不支持
React支持以下三種寫css的方法:
1.行內(nèi)樣式
...
<div style={{
? ? width:'200px',
? ? height:'80px',
? ? backgroundColor:'yellow', ? ?
? ? fontSize:'24px', ? ?
? ? textAlign:'center' ?
}}>測(cè)試數(shù)據(jù)</div>
...
2.聲明樣式
聲明樣式和行內(nèi)樣式類似,區(qū)別只是聲明一個(gè)變量保存樣式表綁定給style屬性。
...
<div style={{
? ? width:'200px',
? ? height:'80px',
? ? backgroundColor:'yellow', ? ?
? ? fontSize:'24px', ? ?
? ? textAlign:'center' ?
}}>測(cè)試數(shù)據(jù)</div>
...
3.import引入
一個(gè)React組件一般就是一個(gè)文件夾,文件夾里包含對(duì)應(yīng)的js和css,只要在js中引入同級(jí)的css即可。
import './mystyle.css';
React中css局部引入
配置Webpack模塊化導(dǎo)入CSS
在新項(xiàng)目創(chuàng)建好時(shí),使用命令yarn eject,顯示配置文件
打開config/webpack.config.js
在檢測(cè)CSS的地方(426行)添加如下
modules: true,
getLocalIdent: getCSSModuleLocalIdent,
使用:
import style from './TestCss.css';
<span className={style.one}>hello</span>
<span id={style.two}>world</span>
在React項(xiàng)目中使用Sass
安裝node-sass包 yarn add node-sass
創(chuàng)建sass文件(以.scss為后綴名,在內(nèi)部寫css代碼)
.TestSass {
span {
? ? color: red;
? ? }
}
引入對(duì)應(yīng)的JS文件或者JSX文件
import 'TestSass.scss'
原文鏈接:https://blog.csdn.net/weixin_39782183/article/details/104751590
相關(guān)推薦
- 2022-02-23 IDEA git 拉取項(xiàng)目時(shí)報(bào) No tracked branch configured for b
- 2022-08-10 .Net使用Cancellation?Framework取消并行任務(wù)_實(shí)用技巧
- 2024-07-13 SpringBoot入門(解決JDK8不存在問(wèn)題)
- 2022-11-23 Python多線程使用方法詳細(xì)講解_python
- 2023-12-10 Failed to process, please exclude the tableName or
- 2023-04-03 PyTorch中Torch.arange函數(shù)詳解_python
- 2023-04-02 pytorch中關(guān)于distributedsampler函數(shù)的使用_python
- 2022-10-18 react電商商品列表的實(shí)現(xiàn)流程詳解_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支