Sass ——媒體指令



說明

@media 指令將樣式規則設定為不同的媒體型別。@media 指令可以巢狀在選擇器 SASS 中,但它被冒泡到樣式表的頂層。

示例

以下示例演示了在 SCSS 檔案中使用 @media 的方法 −

media.htm

<!doctype html>
   <head>
      <title>Media directive Example</title>
      <link rel = "stylesheet" href = "media.css" type = "text/css" />
   </head>

   <body class = "container">
      <h2>Example using media directive</h2>
      <img src = "/sass/images/birds.jpg" class = "style">
   </body>
</html>

接下來,建立檔案 media.scss

media.scss

h2 {
   color: #77C1EF;
}

.style {
   width: 900px;

   @media screen and (orientation: portrait) {
      width:500px;
      margin-left: 120px;
   }
}

您可以透過使用以下命令告訴 SASS 監視檔案並在 SASS 檔案更改時更新 CSS −

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

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

media.css

h2 {
   color: #77C1EF;
 }

.style {
   width: 900px;
}
@media screen and (orientation: portrait) {
   .style {
      width: 500px;
      margin-left: 120px;
   }
}
  

輸出

讓我們執行以下步驟,瞭解上述程式碼如何工作 −

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

  • 在瀏覽器中開啟此 HTML 檔案,輸出如下所示。

Sass rules and directives
sass_rules_and_directives.htm
廣告
© . All rights reserved.