What is nesting in preprocessors?
Nesting in preprocessors is the ability to write selectors as a hierarchy that reflects the HTML structure, instead of repeating the full selector path every time. Nested syntax makes code more compact, more logical and easier to read.
What this looks like in SCSS
scss
.card {
padding: 20px;
.title {
font-size: 18px;
}
.btn {
color: red;
}
}What this turns into after compilation
css
.card {
padding: 20px;
}
.card .title {
font-size: 18px;
}
.card .btn {
color: red;
}Key points of nesting
- selectors are grouped by meaning: everything related to
.cardsits together; - duplication decreases, because you do not have to write
.card .title,.card .btnmanually; - the structure of the styles starts to visually match the structure of the DOM.
Nesting is one of the most noticeable conveniences of preprocessors, and it makes styles cleaner and more organized in large projects.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.