React中css的使用

來源:互聯網
上載者:User

標籤:布局   try   dom   class   網頁   html   檔案   nod   attr   

網頁的布局、顏色、形狀等UI展示方式主要是由Css進行設定,在ReactJs中也是一樣。ReactJs中的Css結構方式與傳統的Web網頁類似,但依然存在一些差異。ReactJs中Css檔案本身的編寫上並沒有差異,我們可以定義特定的樣式類名,也可以正對Html元素定義樣式。差異主要在React對Css的使用上

  • 引用的差異:

傳統html檔案中css檔案引用方式:

<link rel="stylesheet" type="text/css" href="mystyle.css">

ReactJs中css檔案引用方式:

你可以直接採用require:   require(‘./Category.css‘);

你也可以採用import:  import styles from ‘./Category.css‘;

  • 使用差異:

傳統css的使用方式是設定class: <div class=”center”> test </div>

ReactJs中css的使用方式是設定className(因為class已經成為了關鍵字)。

 <div className=‘entry‘>test </div>

另外

如果你採用require的方式,你可以直接在頁面元素中使用css檔案中的樣式名。

如果你採用import styles from ‘./Category.css‘;的方式,則應採用以下方式。

1.<div className={style.entry}>test </div>

2.<div style={{marginTop:10}}>test </div>

3.import ReactDOM from ‘react-dom‘;

<Button ref="button" />

const button = ReactDOM.findDOMNode(this.refs.button);

button.setAttribute(‘disabled‘, ‘true‘); 

  • 最終解析檔案的差異:

傳統css檔案一般依然保持著源碼狀態。

在ReactJS中,Css檔案最終將會對css樣式名進行轉換,例如在index.js檔案中,引用style.css:

.customButton{

}

將轉化為:

.index_customButton_23U0x{

}.

其中23U0x是隨機數,這樣轉化的結果將避免最終的單頁面中,css樣式名不存在同名衝突。

 

React中css的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.