Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Sass 嵌套规则和属性


Sass 嵌套规则

Sass 允许您像在 HTML 中一样嵌套 CSS 选择器。

查看网站导航的一些 Sass 代码示例

示例

SCSS 语法

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  li {
    display: inline-block;
  }
  a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
  }
}

运行示例 »

请注意,在 Sass 中,ullia 选择器嵌套在 nav 选择器中。

而在 CSS 中,规则是逐个定义的(而不是嵌套的)

CSS 语法

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
nav li {
  display: inline-block;
}
nav a {
  display: block;
  padding: 6px 12px;
  text-decoration: none;
}

由于您可以在 Sass 中嵌套属性,因此它比标准 CSS 更简洁易读。



Sass 嵌套属性

许多 CSS 属性具有相同的词缀,例如 font-family、font-size 和 font-weight 或者 text-align、text-transform 和 text-overflow。

使用 Sass,您可以将它们编写为嵌套属性

示例

SCSS 语法

font: {
  family: Helvetica, sans-serif;
  size: 18px;
  weight: bold;
}

text: {
  align: center;
  transform: lowercase;
  overflow: hidden;
}

Sass 编译器会将以上内容转换为正常的 CSS

CSS 输出

font-family: Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;

text-align: center;
text-transform: lowercase;
text-overflow: hidden;


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.