sass在react中的基本使用(實例詳解)
1. 安裝sass
較新的版本不需要配置sass-loader等一系列插件,安裝即用。
npm install --save-dev sass
2. 編寫App.tsx中的基本DOM
更改app.css為app.scss,并刪除其中全部內容
使用如下代碼替換app.tsx中的內容
import "./App.scss"; function App() { return ( <div className="App"> <div className="header"> <ul> <li>導航1</li> <li>導航2</li> <li>導航3</li> </ul> </div> <div className="container"> <div className="con_Item1">測試混入的內容</div> <div className="con_Item2">測試傳參混入</div> <div className="con_Item3">剩余參數(shù)混入</div> <div className="con_Item4">瀏覽器前綴混入</div> <div style={{marginTop:20}}> <div className="baseBtn">基類</div> <div className="reportBtn">繼承1</div> <div className="submitBtn">繼承2</div> </div> </div> <div className="footer">頁腳</div> </div> ); } export default App;
3. sass變量
sass變量使用$
符號開頭 可以存儲字符串、數(shù)字、顏色值、布爾值、列表、null。
下方定義了 若干個sass變量:
$maxWidth: 100%; $maxHeight: 100%; $myColor: red; $myFont: Helvetica, sans-serif; $myFontSize: 20px; $headerHight: 49px; //使用 body, html { width: $maxWidth; height: $maxHeight; margin: 0; padding: 0; } #root { width: $maxWidth; height: $maxHeight; } .App { width: $maxWidth; height: $maxHeight; display: flex; flex-direction: column; }
sass變量只能作用于當前的層級:
$myColor:red; p{ $myColor:green; //只在p中有效果 color:$myColor; //為綠色 } span{ color:$myColor;//為紅色 }
4. sass中的選擇器嵌套和屬性嵌套
選擇器嵌套
.header { width: $maxWidth; height: $headerHight; background-color: gold; ul { margin: 0; padding: 0; height: $maxHeight; list-style: none; display: flex; justify-content: space-around; } li { display: block; flex: 1; height: 100%; line-height: $headerHight; background-color: gold; } }
屬性嵌套
很多 CSS 屬性都有同樣的前綴,例如:font-family, font-size 和 font-weight , text-align, text-transform 和 text-overflow。
在 Sass 中,我們可以使用嵌套屬性來編寫它們:
//屬性嵌套 font: { family: $myFont; size: $myFontSize; weight: bold; } text: { align: center; transform: lowercase; overflow: hidden; }
5. sass中的@import和Partials
@import可以讓我們導入其他文件中的內容
包含文件時不需要指定文件后綴,Sass 會自動添加后綴 .scss。
此外,你也可以導入 CSS 文件。
導入后我們就可以在主文件中使用導入文件等變量。
如在app.scss中導入 :
@import "reset";
reset.scss中的樣式會被編譯到app.scss中
Sass Partials
如果你不希望將一個 Sass 的代碼文件編譯到一個 CSS 文件,你可以在文件名的開頭添加一個下劃線。這將告訴 Sass 不要將其編譯到 CSS 文件。
但是,在導入語句中我們不需要添加下劃線。
我們可以將之前定義的變量抽取出來,粘貼到新建的_globals.scss中
$maxWidth: 100%; $maxHeight: 100%; $myColor: red; $myFont: Helvetica, sans-serif; $myFontSize: 20px; $headerHight: 49px;
然后再app.scss中引入:
@import "./assets/globals"; #root { width: $maxWidth; height: $maxHeight; }
注意:請不要將帶下劃線與不帶下劃線的同名文件放置在同一個目錄下,比如,_colors.scss 和 colors.scss 不能同時存在于同一個目錄下,否則帶下劃線的文件將會被忽略。
6. Sass中的 @mixin 與 @include
@mixin 指令允許我們定義一個可以在整個樣式表中重復使用的樣式。
@include 指令可以將混入(mixin)引入到文檔中。
_globals.scss //sass 混入 @mixin important-redBgWhiteFt { margin: 10px 0; height: 50px; line-height: 50px; text-align: center; background-color: red; color: white; border: 3px solid green; } //接收參數(shù)的混入 @mixin borderInvert($color: white, $width: 2px) { border: $width solid $color; } //剩余參數(shù)混入 @mixin box-shadow($shadows...) { -moz-box-shadow: $shadows; -webkit-box-shadow: $shadows; box-shadow: $shadows; } //瀏覽器前綴使用混入 @mixin transformMix($data) { -webkit-transform: $data; -ms-transform: $data; transform: $data; }
使用:
app.scss //包含基礎混入 .con_Item1 { //sass包含 @include important-redBgWhiteFt; border-radius: 5px; } //包含帶參數(shù)的混入 .con_Item2 { //sass包含 @include borderInvert(blue, 5px); height: 50px; background-color: green; } //包含剩余參數(shù)的混入 .con_Item3 { margin-top: 10px; height: 50px; @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999); } //包含瀏覽器前綴的混入 .con_Item4 { @include borderInvert(gold, 2px); @include transformMix(rotate(180deg)); margin-top: 10px; height: 50px; }
7.sass中的繼承 @extend
@extend 指令告訴 Sass 一個選擇器的樣式從另一選擇器繼承。
如果一個樣式與另外一個樣式幾乎相同,只有少量的區(qū)別,則使用 @extend 就顯得很有用。
//樣式繼承 //被繼承的基類 .baseBtn { @include borderInvert(blue, 1px); margin-top: 20px; display: block; width: 80px; height: 30px; line-height: 30px; text-align: center; cursor: pointer; &:hover{ background-color: gray; } } //繼承基礎樣式的按鈕1 .reportBtn{ @extend .baseBtn; background-color: red; } //繼承基礎樣式的按鈕2 .submitBtn{ @extend .baseBtn; border-radius: 5px; }
8. 源代碼
app.tsx:
import "./App.scss"; function App() { return ( <div className="App"> <div className="header"> <ul> <li>導航1</li> <li>導航2</li> <li>導航3</li> </ul> </div> <div className="container"> <div className="con_Item1">測試混入的內容</div> <div className="con_Item2">測試傳參混入</div> <div className="con_Item3">剩余參數(shù)混入</div> <div className="con_Item4">瀏覽器前綴混入</div> <div style={{marginTop:20}}> <div className="baseBtn">基類</div> <div className="reportBtn">繼承1</div> <div className="submitBtn">繼承2</div> </div> </div> <div className="footer">頁腳</div> </div> ); } export default App;
_globals.scss:
$maxWidth: 100%; $maxHeight: 100%; $myColor: red; $myFont: Helvetica, sans-serif; $myFontSize: 20px; $headerHight: 49px; //sass 混入 @mixin important-redBgWhiteFt { margin: 10px 0; height: 50px; line-height: 50px; text-align: center; background-color: red; color: white; border: 3px solid green; } //接收參數(shù)的混入 @mixin borderInvert($color: white, $width: 2px) { border: $width solid $color; } //剩余參數(shù)混入 @mixin box-shadow($shadows...) { -moz-box-shadow: $shadows; -webkit-box-shadow: $shadows; box-shadow: $shadows; } //瀏覽器前綴使用混入 @mixin transformMix($data) { -webkit-transform: $data; -ms-transform: $data; transform: $data; }
app.scss:
@import "./assets/globals"; body, html { width: $maxWidth; height: $maxHeight; margin: 0; padding: 0; } #root { width: $maxWidth; height: $maxHeight; } .App { width: $maxWidth; height: $maxHeight; display: flex; flex-direction: column; } // 選擇器嵌套 .header { width: $maxWidth; height: $headerHight; background-color: gold; ul { margin: 0; padding: 0; height: $maxHeight; list-style: none; display: flex; justify-content: space-around; } li { display: block; flex: 1; height: 100%; line-height: $headerHight; background-color: gold; //屬性嵌套 font: { family: $myFont; size: $myFontSize; weight: bold; } text: { align: center; transform: lowercase; overflow: hidden; } } } .container { flex: 1; background-color: pink; } //包含基礎混入 .con_Item1 { //sass包含 @include important-redBgWhiteFt; border-radius: 5px; } //包含帶參數(shù)的混入 .con_Item2 { //sass包含 @include borderInvert(blue, 5px); height: 50px; background-color: green; } //包含剩余參數(shù)的混入 .con_Item3 { margin-top: 10px; height: 50px; @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999); } //包含瀏覽器前綴的混入 .con_Item4 { @include borderInvert(gold, 2px); @include transformMix(rotate(180deg)); margin-top: 10px; height: 50px; } //樣式繼承 //被繼承的基類 .baseBtn { @include borderInvert(blue, 1px); margin-top: 20px; display: block; width: 80px; height: 30px; line-height: 30px; text-align: center; cursor: pointer; &:hover{ background-color: gray; } } //繼承基礎樣式的按鈕1 .reportBtn{ @extend .baseBtn; background-color: red; } //繼承基礎樣式的按鈕2 .submitBtn{ @extend .baseBtn; border-radius: 5px; } .footer { width: $maxWidth; height: 49px; text-align: center; line-height: 49px; }
到此這篇關于sass在react中的基本使用的文章就介紹到這了,更多相關react使用sass內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
React如何使用sortablejs實現(xiàn)拖拽排序
這篇文章主要介紹了React如何使用sortablejs實現(xiàn)拖拽排序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01React函數(shù)組件與類組件使用及優(yōu)劣對比
本文主要介紹了React函數(shù)組件與類組件使用及優(yōu)劣對比,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04React+Electron快速創(chuàng)建并打包成桌面應用的實例代碼
這篇文章主要介紹了React+Electron快速創(chuàng)建并打包成桌面應用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12