行内样式
App.js
import React, { Component } from "react";
export default class ClassCom extends Component {
render() {
return (
<div>
<p style={{ color: "red", fontSize: "30px" }}>
我是类组件-行内样式
</p>
</div>
);
}
}
外部样式
App.js
import React, { Component } from "react";
import "../asset/style.css";
export default class ClassCom extends Component {
render() {
return (
<div>
<p className={"waibu"}>我是类组件-外部样式</p>
</div>
);
}
}
asset/style.css
.waibu{
color: blue;
font-size: 20px;
}
使用classname第三方模块
先安装classname
yarn add classname
App.js
import React, { Component } from "react";
import classname from "classname";
import "../asset/style.css";
export default class ClassCom extends Component {
render() {
return (
<div>
<p
className={classname({
waibu: true,
})}
>
我是类组件-使用第三方模块classname
</p>
</div>
);
}
}
asset/style.css
.waibu{
color: blue;
font-size: 20px;
}
使用样式组件styled-component
先安装styled-components
yarn add styled-components
App.js
import React from 'react'
import styled from 'styled-components'
const Container = styled.div`
width: 100px;
height: 100px;
background: #ccc;
`
class Hello extends React.Component{
render () {
return (
{/* Container可以作为组件直接使用 */}
<Container></Container>
)
}
}