<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        react中使用css的7中方式(最全總結)

        來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 22:01:07
        文檔

        react中使用css的7中方式(最全總結)

        react中使用css的7中方式(最全總結):第一種: 在組件中直接使用style 不需要組件從外部引入css文件,直接在組件中書寫。 import React, { Component } from react; const div1 = { width: 300px, margin: 30px auto, backgroundColor: #440
        推薦度:
        導讀react中使用css的7中方式(最全總結):第一種: 在組件中直接使用style 不需要組件從外部引入css文件,直接在組件中書寫。 import React, { Component } from react; const div1 = { width: 300px, margin: 30px auto, backgroundColor: #440

        第一種: 在組件中直接使用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 style={div1}>123</div>
         <div style="background-color:red;">
         );
         }
        }
        
        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 (
         <div>
         <div className="link-name">123</div>
         <TestChidren>測試子組件的樣式</TestChidren>
         </div>
        
         );
         }
        }
        
        export default Test;

        這種方式引入的css樣式,會作用于當前組件及其所有后代組件。

        第三種: 3、在組件中引入[name].scss文件

        引入react內部已經(jīng)支持了后綴為scss的文件,所以只需要安裝node-sass即可,因為有個node-sass,scss文件才能在node環(huán)境上編譯成css文件。

        >yarn add node-sass

        然后編寫scss文件

        //index.scss
        .App{
         background-color: #282c34;
         .header{
         min-height: 100vh;
         color: white;
         }
        }
        

        關于如何詳細的使用sass,請查看sass官網(wǎng)

        這種方式引入的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文件

        類似于第四種,區(qū)別是第四種引入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

        然后創(chuàng)建一個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節(jié)點整體合并成一個組件。引入這個組件html和css都有了。
        它的好處在于可以隨時通過往組件上傳入 屬性,來動態(tài)的改變樣式。對于處理變量、媒體查詢、偽類等較方便的。

        這種方式的css也只對當前組件有效。

        具體用法,請查看styled-components官網(wǎng)

        第七種: 使用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中的數(shù)學,連接,正則表達式,條件,函數(shù)等。

        具體用法請查看radium官網(wǎng)

        注意:

        在export之前,必須用Radium包裹。

        聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        react中使用css的7中方式(最全總結)

        react中使用css的7中方式(最全總結):第一種: 在組件中直接使用style 不需要組件從外部引入css文件,直接在組件中書寫。 import React, { Component } from react; const div1 = { width: 300px, margin: 30px auto, backgroundColor: #440
        推薦度:
        標簽: 的方式 最全 css
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 中文字幕亚洲综合精品一区| 亚洲动漫精品无码av天堂| 中文字幕在线观看亚洲| 国产午夜无码片免费| 亚洲一区二区三区自拍公司| 一级毛片a女人刺激视频免费 | 国产精品亚洲自在线播放页码| 久久成人国产精品免费软件| 亚洲一区在线免费观看| 亚洲高清中文字幕免费| 亚洲日韩AV一区二区三区四区 | 亚洲av无码一区二区三区天堂古代 | 日本视频一区在线观看免费| 国产精品高清视亚洲精品| 成年女人毛片免费播放人| 极品色天使在线婷婷天堂亚洲| 亚洲av女电影网| 免费人妻无码不卡中文字幕系| 亚洲成人高清在线观看| 成人免费福利电影| 免费福利在线观看| 久久亚洲精品视频| 69式互添免费视频| 三年片在线观看免费观看大全中国 | 亚洲成a人片在线网站| 成年女人18级毛片毛片免费| 男性gay黄免费网站| 91视频国产免费| 国产精品久久亚洲一区二区| 国产亚洲?V无码?V男人的天堂| 鲁大师在线影院免费观看| 亚洲乱码中文字幕在线| 亚洲午夜久久久久久噜噜噜| 1024免费福利永久观看网站| 羞羞的视频在线免费观看| 亚洲黄色片在线观看| 四虎影视永久免费观看网址| a级成人毛片免费图片| 亚洲中文字幕无码mv| 亚洲综合无码AV一区二区| 国产成人免费网站|