目錄
#透過使用 @use 和 partials
After all the sub files are linked or included into the common file, you need to run the below command in the same folder where all SASS files exists −
- 在最後一步中,我們將使用上述命令將公共文件包含或連結到最終的CSS文件中,然後將其與HTML文件連結。
In the above example, we have used the final final.css file to style the document with all the styles of SASS files.
使用
方法嵌入樣式與
首頁 web前端 css教學 SASS 中的文件分割

SASS 中的文件分割

Sep 01, 2023 pm 02:29 PM

SASS 中的文件分割

SASS is a CSS pre-processor, that stands for Syntactically Awesome Style Sheet. The SASS code is written just like a scripting language like JavaScript, but at the time of compilation it is converted into CSS and compiled as CSS in the browser. SASS can be used with any version of CSS. It is used to enhance and advance the way of writing the code in normal.

##在正常的工作空間中,我們習慣將整個程式碼寫在一個單獨的檔案中,這使得我們的程式碼對其他開發人員來說難以閱讀和理解。 SASS允許我們將文件拆分並將程式碼分成多個文件。

The process of splitting a file includes the breaking of a one big file into multiple sub files and then link them with each other, which can be easily done by using the below methods in SASS −

  • ##By using @import and partials

#透過使用 @use 和 partials

讓我們現在詳細了解上述方法,透過程式碼範例來連結SASS中單一檔案的多個子檔案。

透過使用@import和部分檔案

In this method, we will write the styles as we normally writes in CSS files. But there will be a common file that contains the

@import

statement for all the other files to include or link them toge and get the code of all those files in that file.

After all the sub files are linked or included into the common file, you need to run the below command in the same folder where all SASS files exists −

Sass –-watch common_file_name.scss final_output_file_name.scss
登入後複製
    The above command will link or include the whole code of the common SASS file into the final output file that will be linked to the HTML document to style the page.
  • 讓我們透過程式碼範例詳細討論上述方法的實作方式 - 步驟

  • Step 1 − In this step, we will create multiple SASS file with .scss extension

  • 第二步 - 現在,我們將建立一個包含在上一個步驟中建立的所有SASS檔案的@import語句的SASS檔案。

第三步驟

- 在最後一步中,我們將使用上述命令將公共文件包含或連結到最終的CSS文件中,然後將其與HTML文件連結。

  • Explanation 的中文翻譯為:解釋

File 1 − let us create a file named test.scss and put some SASS code inside that file.

  • #test.css −#

    div{
       color: #fff;
       background: #444;
       margin: 15px;
    }
    
    登入後複製

File 2

− Now, create a file named common.scss. This file will link all the sub files using the @import statement.

  • common.scss −

    @import "test.scss";
    div{
       font-size: 22px;
       font-weight: bold;
       padding: 15px;
    }
    
    登入後複製

檔案3 − 這將是我們的最終檔案final.css,其中包含所有的SASS程式碼,並且將連結到HTML文件。

Run below command −

sass –-watch common.scss final.css
登入後複製
登入後複製

final.css −

final.css:
/* imported code from test.scss */
div{
   color: #fff;
   background: #444;
   margin: 15px;
}
/* code from common.scss */
div{
   font-size: 22px;
   font-weight: bold;
   padding: 15px;
}
登入後複製

Now, we can link the

final.css

file with the HTML document to style the page with the CSS of all the SASS files as done in the below example.

Example The below example will you how you can create and link multiple SASS file together and style a page −

#
<html>
<head>
   <style>
      /* imported code from test.scss */
      div{
         color: #fff;
         background: #444;
         margin: 15px;
      }
      /* code from common.scss */
      div{
         font-size: 22px;
         font-weight: bold;
         padding: 15px;
      }
   </style>
</head>
<body>
   <div>
      <h2>This is Heading of First Div.</h2>
   </div>
   <div>
      <h2>This is Heading of Second Div.</h2>
   </div>
</body>
</html>
登入後複製

In the above example, we have used the final final.css file to style the document with all the styles of SASS files.

注意 - 請確保您的系統中已經預先安裝了SASS,以實現上述程式碼範例。 透過使用@use和局部檔案

使用

@use

方法嵌入樣式與

@import
    方法幾乎相似。您只需要在檔案名稱前面加上底線作為前綴,並使用@use語句匯入它們。這也將允許我們存取在SASS檔案中定義的函數和混合。
  • Explanation

    的中文翻譯為:
  • 解釋

    File 1 − File 1 will be a SASS file that contains the functions, mixins and simple CSS styles defined with a underscore as prefix.
    • #_test.scss −

      @function my_space(){
         $padding: "15px";
         return $padding;
      }
      
      登入後複製

    文件 2 − 這將會是一個常見的文件,使用 @use 語句將所有的 SASS 檔案連結在一起。

    • common.scss

      @use "test";
      div{
         color: #fff;
         padding: test.my_space();
      }
      
      登入後複製

    文件3 − 這個文件是最終的CSS文件,它是來自所有SASS文件的所有樣式的最終版本。

    Run below command −

    sass –-watch common.scss final.css
    
    登入後複製
    登入後複製

    final.css −

/* combined code from both files */
div{
   color: #fff;
   padding: 15px;
}
登入後複製
###In this way you can implement the SASS by splitting the files and add styles to the HTML document with a final outputting CSS file.### ###在本文中,我們學習了兩種將拆分的SASS檔案連結或嵌入到一個單獨檔案中,並使用該最終的CSS檔案為我們的HTML頁面新增樣式的方法。 ### ######

以上是SASS 中的文件分割的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

使用GraphQL緩存 使用GraphQL緩存 Mar 19, 2025 am 09:36 AM

如果您最近開始使用GraphQL或審查了其優點和缺點,那麼您毫無疑問聽到了諸如“ GraphQl不支持緩存”或

使用Redwood.js和Fauna構建以太坊應用 使用Redwood.js和Fauna構建以太坊應用 Mar 28, 2025 am 09:18 AM

隨著最近比特幣價格超過20k美元的攀升,最近打破了3萬美元,我認為值得深入研究創建以太坊

VUE 3 VUE 3 Apr 02, 2025 pm 06:32 PM

它的出局!恭喜Vue團隊完成了完成,我知道這是一項巨大的努力,而且很長時間。所有新文檔也是如此。

用高架創建自己的野蠻人 用高架創建自己的野蠻人 Mar 18, 2025 am 11:23 AM

無論您是開發人員的哪個階段,我們完成的任務(無論大小)都會對我們的個人和專業成長產生巨大影響。

您可以從瀏覽器獲得有效的CSS屬性值嗎? 您可以從瀏覽器獲得有效的CSS屬性值嗎? Apr 02, 2025 pm 06:17 PM

我有人寫了這個非常合法的問題。 Lea只是在博客上介紹瞭如何從瀏覽器中獲得有效的CSS屬性。那樣的是這樣。

在CI/CD上有點 在CI/CD上有點 Apr 02, 2025 pm 06:21 PM

我說的“網站”比“移動應用程序”更合適,但我喜歡Max Lynch的框架:

比較瀏覽器的響應式設計 比較瀏覽器的響應式設計 Apr 02, 2025 pm 06:25 PM

這些桌面應用程序中有許多目標是同時在不同的維度上顯示您的網站。因此,例如,您可以寫作

帶有粘性定位的堆疊卡和一點點的雜物 帶有粘性定位的堆疊卡和一點點的雜物 Apr 03, 2025 am 10:30 AM

前幾天,我發現了科里·金尼文(Corey Ginnivan)網站上的這一點,當您滾動時,彼此之間的卡片堆放集。

See all articles