Sass - 巢狀屬性



說明

使用巢狀屬性,您可以避免多次重寫 CSS。例如,把font當做名稱空間,它使用一些屬性,例如字體系列、字型大小、字型粗細和字型變體。在普通 CSS 中,您需要每次都用名稱空間編寫這些屬性。使用 SASS,您可以只編寫一次名稱空間來巢狀屬性。

示例

以下示例描述在 SCSS 檔案中使用巢狀屬性:

<html>
   <head>
      <title>Nested Properties</title>
      <link rel = "stylesheet" type = "text/css" href = "style.css" />
      <link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
      <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
   </head>

   <body>
      <div class = "container">
         <h1>Example using Nested Properties</h1>
         <p class = "line">SASS stands for Syntactically Awesome Stylesheet</p>
      </div>
   </body>
</html>

接下來,建立檔案style.scss

style.scss

.line {
   font: {
      family: Lucida Sans Unicode;
      size:20px;
      weight: bold;
      variant: small-caps;
   }
}

您可以讓 Sass 監視檔案,並且每當 SASS 檔案發生變化時都更新 CSS,方法是使用以下命令:

sass --watch C:\ruby\lib\sass\style.scss:style.css

接下來,執行上述命令;它將自動建立style.css檔案,其中包含以下程式碼:

style.css

.line {
   font-family: Lucida Sans Unicode;
   font-size: 20px;
   font-weight: bold;
   font-variant: small-caps;
}

輸出

讓我們執行以下步驟,看看上面給定的程式碼如何工作:

  • 將上述給定的 html 程式碼儲存在nested_properties.html檔案中。

  • 在這個 HTML 檔案中開啟一個瀏覽器,將顯示如下所示的輸出。

Sass CSS Extensions
sass_css_extensions.htm
廣告
© . All rights reserved.