- Sass 教程
- Sass - 主頁
- Sass - 概覽
- Sass - 安裝
- Sass - 語法
- 使用 Sass
- Sass - CSS 擴充套件
- Sass - 註釋
- Sass - 指令碼
- Sass - @ 規則和指令
- 控制指令和表示式
- Sass - 混合指令
- Sass - 函式指令
- Sass - 輸出樣式
- 擴充套件 Sass
- Sass 實用資源
- Sass - 面試問題
- Sass - 快速指南
- Sass - 有用的資源
- Sass - 討論
Sass - 巢狀規則
說明
巢狀是將不同的邏輯結構進行組合。使用 SASS,我們可以將多個 CSS 規則進行組合。如果要使用多個選擇器,那麼可以使用一個選擇器巢狀另一個選擇器以建立複合選擇器。
示例
以下示例介紹了在 SCSS 檔案中使用巢狀規則——
<html>
<head>
<title>Nested Rules</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>My First Heading</h1>
<p>It is a CSS pre-processor which helps to reduce repetition with CSS and save the time. </p>
<p>It is more stable and powerful CSS extension language.</p>
<div class = "box">
<h1>My Second Heading</h1>
<p>It is initially designed by Hampton Catlin and developed by Natalie Weizenbaum in 2006.</p>
</div>
</div>
</body>
</html>
接下來,建立檔案 style.scss。請注意 .scss 擴充套件。
style.scss
.container{
h1{
font-size: 25px;
color:#E45456;
}
p{
font-size: 25px;
color:#3C7949;
}
.box{
h1{
font-size: 25px;
color:#E45456;
}
p{
font-size: 25px;
color:#3C7949;
}
}
}
你還可以指示 SASS 監聽檔案,並在 SASS 檔案發生改變後更新 CSS,方法是使用以下命令——
sass --watch C:\ruby\lib\sass\style.scss:style.css
接下來,執行以上命令,系統將自動建立 style.css 檔案,其中包含以下程式碼——
生成的 style.css 如下所示——
style.css
.container h1 {
font-size: 25px;
color: #E45456;
}
.container p {
font-size: 25px;
color: #3C7949;
}
.container .box h1 {
font-size: 25px;
color: #E45456;
}
.container .box p {
font-size: 25px;
color: #3C7949;
}
輸出
讓我們執行以下步驟以瞭解上面給定的程式碼是如何工作的——
將上面給定的 html 程式碼儲存到 nested_rules.html 檔案中。
在瀏覽器中開啟此 HTML 檔案,會顯示一個如下所示的輸出。
sass_css_extensions.htm
廣告