Sass nesting rules and properties
Sass nested CSS selectors are similar to HTML's nested rules.
As follows we nest a navigation bar style:
Sass code:
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
display: inline-block;
}
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
In the example, the ul, li, and a selectors are all nested inside the nav selector
Convert the above code to CSS code as follows:
CSS code:
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 nested properties
Many CSS properties have the same prefix, for example: font-family, font-size and font-weight, text-align, text-transform and text-overflow.
In Sass, we can write them using nested properties:
Sass code:
font: {
family: Helvetica, sans-serif;
size: 18px;
weight: bold;
}
text: {
align: center;
transform: lowercase;
overflow: hidden;
}
Convert the above code to CSS code as follows:
CSS code:
font-family: Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
text-align: center;
text-transform: lowercase;
text-overflow: hidden;