99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

react中如何使用css

這篇文章主要介紹了react中如何使用css,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

創新互聯建站是一家專業提供麻章企業網站建設,專注與成都做網站、網站制作、成都外貿網站建設H5頁面制作、小程序制作等業務。10年已為麻章眾多企業、政府機構等服務。創新互聯專業網站制作公司優惠進行中。

第一種: 在組件中直接使用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中如何使用css

而在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包裹。

感謝你能夠認真閱讀完這篇文章,希望小編分享react中如何使用css內容對大家有幫助,同時也希望大家多多支持創新互聯,關注創新互聯行業資訊頻道,遇到問題就找創新互聯,詳細的解決方法等著你來學習!

當前標題:react中如何使用css
文章源于:http://www.yijiale78.com/article8/pehdip.html

成都網站建設公司_創新互聯,為您提供微信公眾號手機網站建設做網站自適應網站企業網站制作移動網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

商城網站建設