Sass 嵌套屬性

2018-12-30 15:21 更新

描述

使用嵌套屬性可以避免重寫CSS多次。例如,使用 font 作為使用一些屬性(例如font-family,font-size,font-weight和font-variant)的命名空間。在正常的CSS中,您需要每次使用命名空間來(lái)編寫這些屬性。使用SASS,您可以通過(guò)僅寫入命名空間一次來(lái)嵌套屬性。

例子

以下示例描述了在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="/attachments/tuploads/sass/jquery.min.js"></script>
   <script src="/attachments/tuploads/sass/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>

接下來(lái),創(chuàng)建文件 style.scss 。

style.scss

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

您可以通過(guò)使用以下命令讓SASS查看文件并在SASS文件更改時(shí)更新CSS:

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

接下來(lái)執(zhí)行上面的命令,它將用下面的代碼自動(dòng)創(chuàng)建 style.css 文件:

style.css

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

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上述html代碼保存在 nested_properties.html 文件中。

  • 在瀏覽器中打開此HTML文件,將顯示如下輸出。

Sass CSS Extensions
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)