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="background-color:red;">
</div>
);
}
}
export default Test;
注意事项:
在正常的css中,比如background-color,box-sizing等属性,在style对象div1中的属性中,必须转换成驼峰法,backgroundColor,boxSizing。而没有连字符的属性,如margin,width等,则在style对象中不变。
在正常的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;
}
而在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样式,会作用于当前组件及其所有后代组件。
第三种: 在组件中引入[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.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;
这种方式是将整个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包裹。
React中使用CSS的更多相关文章
- 在React中使用CSS Modules设置样式
最近,一直在看React...那真的是一个一直在学的过程啊,从配置环境webpack,到基础知识jsx,babel,es6,没有一个不是之前没有接触的.其实,我内心是兴奋的啊,毕竟,活着就是要接触一些 ...
- 在react中实现CSS模块化
react中使用普通的css样式表会造成作用域的冲突,css定义的样式的作用域是全局,在Vue 中我们还可以使用scope来定义作用域,但是在react中并没有指令一说,所以只能另辟蹊径了.下面我将简 ...
- React中引用CSS样式的方法
相对于html中引用css的三种方法,react中也有三种方法,一一相对: 1. 行内样式:直接在组件内部定义 <div style={{width:'20px',height:'30px'}} ...
- 聊一聊 React 中的 CSS 样式方案
和 Angular,Vue 不同,React 并没有如何在 React 中书写样式的官方方案,依靠的是社区众多的方案.社区中提供的方案有很多,例如 CSS Modules,styled-compone ...
- react中antd+css Module一起使用
antd 和 css modules 不能混用,针对antd的css 单独写一条loader的规则,不开启 css modules. 使用 exclude 和 include 配置参考(https:/ ...
- react中使用css动画效果
index.js import React, { Component, Fragment } from 'react'; class App extends Component { construct ...
- react中实现css动画
- Angular Vue React 框架中的 CSS
框架中的 CSS Angular Vue React 三大框架 Angular Vue 内置样式集成 React 一些业界实践 Angular Angular . js (1.x):没有样式集成能力 ...
- react中怎么写css样式?
JSX基本语法中关于react如何写css样式主要有三种方法 1.基于class --(className) 基于className ,通过className在style中给该class名的DOM元素 ...
随机推荐
- Java中的IO流,Input和Output的用法,字节流和字符流的区别
Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...
- Functional and Class Components
[Functional and Class Components] The simplest way to define a component is to write a JavaScript fu ...
- DRDS 概述
DRDS 概述 更新时间:2017-08-04 13:53:50 分布式关系型数据库服务(Distributed Relational Database Service , 简称 DRDS ) ...
- Python 面向对象基础(类、实例、方法、属性封装)
python是面向对象语言,一切皆对象. 面向过程: 变量和函数. “散落” 在文件的各个位置,甚至是不同文件中.看不出变量与函数的相关性,非常不利于维护,设计模式不清晰. 经常导致程序员,忘记某个变 ...
- gitlab-ce 安装、汉化与阿里邮箱配置(注意是CE)
环境准备 yum install curl openssh-server openssh-clients postfix cronie policycoreutils-python –y curl h ...
- unity中Camera.ScreenToWorldPoint
Camera.ScreenToWorldPointVector3 ScreenToWorldPoint(Vector3 position); 将屏幕坐标转换为世界坐标. 如何转换?假如给定一个所谓的屏 ...
- javascript简单的选项卡
实现一个简单的选项卡功能 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- vue动态绑定类样式ClassName知多少
对于动态绑定类样式,之前用的最多的也就是:class="{'classA':true}" ,今天遇到一种情况,就是要给元素动态添加一个保存在数据源中的类样式,那前边的这种写法显然满 ...
- jstl的forEach详解(转)
<c:forEach>标签用于通用数据循环,它有以下属性 属 性 描 述 是否必须 缺省值 items 进行循环的项目 否 无 begin 开始条件 否 0 end 结束条件 否 集合中的 ...
- sqlserver DBA面试题
1.sqlserver 2008 R2 on windows server 2008 R2群集中,有节点A.B,现在需要停机新添加一个节点C进来替换现有节点B,请列出必要的步骤. 2.sqlserve ...