网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

react中怎么使用css-创新互联

这篇文章给大家分享的是有关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 (
   123
      );  } } export default Test;

注意事项:

  1. 在正常的css中,比如background-color,box-sizing等属性,在style对象div1中的属性中,必须转换成驼峰法,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;
}

而在react中使用style对象的方式时。值必须用双引号包裹起来。

这种方式的react样式,只作用于当前组件。

第二种: 在组件中引入[name].css文件

需要在当前组件开头使用import引入css文件。

import React, { Component } from "react";
import TestChidren from "./TestChidren";
import "@/assets/css/index.scss";

class Test extends Component {
 constructor(props, context) {
  super(props);
 }
 
 render() {
  return (
   
    123
    测试子组件的样式    
  );  } } 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 (
   
    321321
        
  );  } } 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 (
   
    321321
           );  } } 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 (
   
    app.js           SelfButton         
  );  } } 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 (
   
        this is a primary button        
  );  } } export default Radium(Test);

对于处理变量、媒体查询、伪类等是不方便的。

使用Radium可以直接处理变量、媒体查询、伪类等,并且可以直接使用js中的数学,连接,正则表达式,条件,函数等。

感谢各位的阅读!关于“react中怎么使用css”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


网站题目:react中怎么使用css-创新互联
文章出自:http://bjjierui.cn/article/gpjeo.html

其他资讯