第一種: 在組件中直接使用style
不需要組件從外部引入css文件,直接在組件中書寫晴竞。
import React, { Component } from "react";
const div1 = {
width: "300px",
margin: "30px auto",
backgroundColor: "#44014C", //駝峰法
minHeight: "200px",
boxSizing: "border-box"
};
class Test extends Component {
constructor(props, context) {
super(props);
}
render() {
return (
<div>
<div style={div1}>123</div>
<div style="background-color:red;">
</div>
);
}
}
export default Test;
注意事項(xiàng):
在正常的css中蛙卤,比如background-color,box-sizing等屬性噩死,在style對(duì)象div1中的屬性中颤难,必須轉(zhuǎn)換成駝峰法,backgroundColor已维,boxSizing行嗤。而沒有連字符的屬性,如margin垛耳,width等栅屏,則在style對(duì)象中不變。
在正常的css中堂鲜,css的值不需要用雙引號(hào)("")栈雳,如
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
而在react中使用style對(duì)象的方式時(shí)。值必須用雙引號(hào)包裹起來缔莲。
這種方式的react樣式哥纫,只作用于當(dāng)前組件。
第二種: 在組件中引入[name].css文件
需要在當(dāng)前組件開頭使用import引入css文件酌予。
import React, { Component } from "react";
import TestChidren from "./TestChidren";
import "@/assets/css/index.css";
class Test extends Component {
constructor(props, context) {
super(props);
}
render() {
return (
<div>
<div className="link-name">123</div>
<TestChidren>測(cè)試子組件的樣式</TestChidren>
</div>
);
}
}
export default Test;
這種方式引入的css樣式磺箕,會(huì)作用于當(dāng)前組件及其所有后代組件奖慌。
第三種: 在組件中引入[name].scss文件
引入react內(nèi)部已經(jīng)支持了后綴為scss的文件抛虫,所以只需要安裝node-sass即可,因?yàn)橛袀€(gè)node-sass简僧,scss文件才能在node環(huán)境上編譯成css文件建椰。
>yarn add node-sass
然后編寫scss文件
//index.scss
.App{
background-color: #282c34;
.header{
min-height: 100vh;
color: white;
}
}
關(guān)于如何詳細(xì)的使用sass,請(qǐng)查看sass官網(wǎng)
這種方式引入的css樣式岛马,同樣會(huì)作用于當(dāng)前組件及其所有后代組件棉姐。
第四種: 在組件中引入[name].module.css文件
將css文件作為一個(gè)模塊引入,這個(gè)模塊中的所有css啦逆,只作用于當(dāng)前組件伞矩。不會(huì)影響當(dāng)前組件的后代組件。
import React, { Component } from "react";
import TestChild from "./TestChild";
import moduleCss from "./test.module.css";
class Test extends Component {
constructor(props, context) {
super(props);
}
render() {
return (
<div>
<div className={moduleCss.linkName}>321321</div>
<TestChild></TestChild>
</div>
);
}
}
export default Test;
這種方式可以看做是前面第一種在組件中使用style的升級(jí)版夏志。完全將css和組件分離開乃坤,又不會(huì)影響其他組件。
第五種: 在組件中引入 [name].module.scss文件
類似于第四種,區(qū)別是第四種引入css module湿诊,而這種是引入 scss module而已狱杰。
import React, { Component } from "react";
import TestChild from "./TestChild";
import moduleCss from "./test.module.scss";
class Test extends Component {
constructor(props, context) {
super(props);
}
render() {
return (
<div>
<div className={moduleCss.linkName}>321321</div>
<TestChild></TestChild>
</div>
);
}
}
export default Test;
同樣這種方式可以看做是前面第一種在組件中使用style的升級(jí)版。
第六種: 使用styled-components
需要先安裝
>yarn add styled-components
然后創(chuàng)建一個(gè)js文件(注意是js文件厅须,不是css文件)
//style.js
import styled, { createGlobalStyle } from "styled-components";
export const SelfLink = styled.div`
height: 50px;
border: 1px solid red;
color: yellow;
`;
export const SelfButton = styled.div`
height: 150px;
width: 150px;
color: ${props => props.color};
background-image: url(${props => props.src});
background-size: 150px 150px;
`;
組件中使用styled-components樣式
import React, { Component } from "react";
import { SelfLink, SelfButton } from "./style";
class Test extends Component {
constructor(props, context) {
super(props);
}
render() {
return (
<div>
<SelfLink title="People's Republic of China">app.js</SelfLink>
<SelfButton color="palevioletred" style={{ color: "pink" }} src={fist}>
SelfButton
</SelfButton>
</div>
);
}
}
export default Test;
這種方式是將整個(gè)css樣式仿畸,和html節(jié)點(diǎn)整體合并成一個(gè)組件。引入這個(gè)組件html和css都有了朗和。它的好處在于可以隨時(shí)通過往組件上傳入 屬性错沽,來動(dòng)態(tài)的改變樣式。對(duì)于處理變量眶拉、媒體查詢甥捺、偽類等較方便的。
這種方式的css也只對(duì)當(dāng)前組件有效镀层。具體用法镰禾,請(qǐng)查看styled-components官網(wǎng)。
第七種: 使用radium
需要先安裝
>yarn add radium
然后在react組件中直接引入使用
import React, { Component } from "react";
import Radium from 'radium';
let styles = {
base: {
color: '#fff',
':hover': {
background: '#0074d9'
}
},
primary: {
background: '#0074D9'
},
warning: {
background: '#FF4136'
}
};
class Test extends Component {
constructor(props, context) {
super(props);
}
render() {
return (
<div>
<button style={[ styles.base, styles.primary ]}>
this is a primary button
</button>
</div>
);
}
}
export default Radium(Test);
對(duì)于處理變量唱逢、媒體查詢吴侦、偽類等是不方便的。使用Radium可以直接處理變量坞古、媒體查詢备韧、偽類等,并且可以直接使用js中的數(shù)學(xué)痪枫,連接凶赁,正則表達(dá)式锌云,條件,函數(shù)等。
具體用法請(qǐng)查看radium源碼
注意:在export之前萝挤,必須用Radium包裹滤灯。
END
感興趣的小伙伴添加公眾號(hào) 【grain先森】巧娱,后臺(tái)回復(fù) 【190213】赌躺,免費(fèi)領(lǐng)取30本經(jīng)典編程書籍。