[SCSS] Organize SCSS into Multiple Files with Partials

來源:互聯網
上載者:User

標籤:mode   back   define   learn   style   mono   with   art   com   

Tired of dealing with monolithic CSS files? Are requests for multiple CSS files hurting your performance? In this lesson we learn how to separate our styles with SCSS partials, and how SCSS imports compile to one file so there‘s only one request.

 

Things to know:

  • If importing partial file, won‘t generate a new css file
  • If importing normal scss file, it will generate a new css file

So when we should use partial import or not, you can think that whether this file should be used when app first loading? If it yes, then use partial import, if not, then normal import. The reason behind that is because browser will loading main css (the main.scss file that import other scss files ) file first. After this main.css file loaded, then browser will start loading other css file. 

As you can see other.css which are import as normal import mode. "Content downloaded" happens after main.css file are downloaded.

 

  • Only partial file‘s variable can be shared accross the rest of partial file. But it also require you import that partial file which has variables defines into main.scss file before the rest partial files.
/** About using variable There are tow cases when using variable 1. none partial file (provider) + none partial file    Variable only available for its own file scope.    If you want to use one variable inside file A from file B    You have to import file A into file B. 2. partial file (provider) + none partial file    If you have an variable defined in partial file, and you want to    use it inside none partial file, you also need to import partial    file into none partial file. 3. partial file (provider) + partial file    If you have an variable defined in partial file A, and you want to use it    inside another partial file B, you have to import file A into main.scss file    before you import file B into main.scss. Then you can use variable inside file A    inside file B.*/@import "color";@import "partial";@import "other";.color {  color: $primary-color; // From _color}

 

_color.scss

$primary-color: lighten(red, 15%);.red {  color: $primary-color;}

 

_partial.scss

.l-border {  border-left: 5px solid $primary-color;}

 

other.scss

@import "color";.bg {    background-color: $primary-color;}

 

[SCSS] Organize SCSS into Multiple Files with Partials

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.