本篇文章给人人带来的内容是关于react中运用css的七种要领引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。
第一种: 在组件中直接运用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={{backgroundColor:"red"}}> </div> ); } } export default Test;
注重事项:
1.在一般的css中,比方background-color,box-sizing等属性,在style对象p1中的属性中,必需转换成驼峰法,backgroundColor,boxSizing。而没有连字符的属性,如margin,width等,则在style对象中稳定。
2.在一般的css中,css的值不须要用双引好(""),如
.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; }
3.不能直接运用字符串写style,会报错
<div style="background-color:red">
而在react中运用style对象的体式格局时。值必需用双引号包裹起来。
这类体式格局的react款式,只作用于当前组件。
第二种: 在组件中引入[name].css文件
须要在当前组件开首运用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>测试子组件的款式</TestChidren> </div> ); } } export default Test;
这类体式格局引入的css款式,会作用于当前组件及其一切子女组件。
第三种: 3、在组件中引入[name].scss文件
react内部已支撑了后缀为scss的文件,所以只须要装置node-sass即可,由于有了node-sass,scss文件才能在node环境上编译成css文件。
>yarn add node-sass
然后编写scss文件
//index.scss .App{ background-color: #282c34; .header{ min-height: 100vh; color: white; } }
关于怎样细致的运用sass,请检察sass官网
这类体式格局引入的css款式,一样会作用于当前组件及其一切子女组件。
第四种: 在组件中引入[name].module.css文件
将css文件作为一个模块引入,这个模块中的一切css,只作用于当前组件。不会影响当前组件的子女组件。
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的升级版。完整将css和组件分脱离,又不会影响其他组件。
第五种: 在组件中引入 [name].module.scss文件
类似于第四种,区别是第四种引入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的升级版。
第六种: 运用styled-components
须要先装置
>yarn add styled-components
然后建立一个js文件(注重是js文件,不是css文件)
//style.js import styled, { createGlobalStyle } from "styled-components"; export const SelfLink = styled.p` height: 50px; border: 1px solid red; color: yellow; `; export const SelfButton = styled.p` 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;
这类体式格局是将悉数css款式,和html节点团体合并成一个组件。引入这个组件html和css都有了。
它的优点在于能够随时通过往组件上传入 属性,来动态的转变款式。关于处置惩罚变量、媒体查询、伪类等较轻易的。
这类体式格局的css也只对当前组件有用。
详细用法,请检察styled-components官网
第七种: 运用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);
关于处置惩罚变量、媒体查询、伪类等是不轻易的。
运用Radium能够直接处置惩罚变量、媒体查询、伪类等,而且能够直接运用js中的数学,衔接,正则表达式,前提,函数等。
详细用法请检察radium官网
注重:在export之前,必需用Radium包裹。
本篇文章到这里就已悉数完毕了,更多其他精彩内容能够关注ki4网的CSS视频教程栏目!
以上就是react中运用css的七种要领引见(附代码)的细致内容,更多请关注ki4网别的相干文章!
上一篇:CSS完成基于用户转动运用(代码)【html5教程】,css3,javascript
下一篇:Centos7装置和设置Mysql5.7的要领分享【MySQL教程】,Mysql5.7,Centos7,方法
支付宝扫一扫打赏
微信扫一扫打赏
共 0 条评论关于"react中运用css的七种要领引见(附代码)【html5教程】,react.js,css3,scss"
最新评论