網站首頁 編程語言 正文
React引入css的方法
<div style="color:red">測試數據</div> //報錯
在React中,如果直接按上面的方式寫內聯樣式會直接報錯,因為JSX語法不支持
React支持以下三種寫css的方法:
1.行內樣式
...
<div style={{
? ? width:'200px',
? ? height:'80px',
? ? backgroundColor:'yellow', ? ?
? ? fontSize:'24px', ? ?
? ? textAlign:'center' ?
}}>測試數據</div>
...
2.聲明樣式
聲明樣式和行內樣式類似,區別只是聲明一個變量保存樣式表綁定給style屬性。
...
<div style={{
? ? width:'200px',
? ? height:'80px',
? ? backgroundColor:'yellow', ? ?
? ? fontSize:'24px', ? ?
? ? textAlign:'center' ?
}}>測試數據</div>
...
3.import引入
一個React組件一般就是一個文件夾,文件夾里包含對應的js和css,只要在js中引入同級的css即可。
import './mystyle.css';
React中css局部引入
配置Webpack模塊化導入CSS
在新項目創建好時,使用命令yarn eject,顯示配置文件
打開config/webpack.config.js
在檢測CSS的地方(426行)添加如下
modules: true,
getLocalIdent: getCSSModuleLocalIdent,
使用:
import style from './TestCss.css';
<span className={style.one}>hello</span>
<span id={style.two}>world</span>
在React項目中使用Sass
安裝node-sass包 yarn add node-sass
創建sass文件(以.scss為后綴名,在內部寫css代碼)
.TestSass {
span {
? ? color: red;
? ? }
}
引入對應的JS文件或者JSX文件
import 'TestSass.scss'
原文鏈接:https://blog.csdn.net/weixin_39782183/article/details/104751590
相關推薦
- 2022-06-02 OnZoom基于Apache?Hudi的一體架構實踐解析_服務器其它
- 2022-12-11 centos離線安裝mongodb-database-tools方法詳解_MongoDB
- 2024-04-04 linux設置串口波特率和讀取
- 2022-07-03 對比分析BN和dropout在預測和訓練時區別_python
- 2022-07-09 python處理excel文件之xlsxwriter?模塊_python
- 2022-11-25 CentOS?7.9?升級內核?kernel-ml-5.6.14版本的方法_云其它
- 2022-07-10 移動文件夾ubuntu
- 2022-04-28 一篇文章帶你了解C++特殊類的設計_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支