Sass Nesting Rules and Properties
Sass nesting of CSS selectors is similar to HTML's nesting rules.
Below 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 within the nav selector
Convert the above code into CSS code as shown below:
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 share the same prefix, for example: font-family, font-size, and font-weight; text-align, text-transform, and text-overflow.
In Sass, we can use nested properties to write them:
Sass code:
font
:
{
family
:
Helvetica
,
sans-serif
;
size
:
18px
;
weight
:
bold
;
}
text
:
{
align
:
center
;
transform
:
lowercase
;
overflow
:
hidden
;
}
Convert the above code into CSS code as shown below:
CSS code:
font-family
:
Helvetica
,
sans-serif
;
font-size
:
18px
;
font-weight
:
bold
;
text-align
:
center
;
text-transform
:
lowercase
;
text-overflow
:
hidden
;