SCSS Syntax and Structure
1. SCSS vs Sass Syntax - Differences and Usage
| Feature | SCSS Syntax | Sass (Indented) Syntax | Notes |
|---|---|---|---|
| File Extension | .scss |
.sass |
SCSS is CSS-compatible superset |
| Braces & Semicolons | Required { } ; |
Not used (indentation-based) | SCSS more familiar to CSS developers |
| Nesting | Curly braces { } |
Indentation (2 spaces) | Both support same nesting features |
| Mixins | @mixin name { } |
=name |
Sass uses = and + shorthand |
| Include | @include name; |
+name |
Both compile to same CSS |
| Comments | // or /* */ |
// or /* */ |
Same comment syntax |
Example: SCSS vs Sass comparison
// SCSS Syntax (.scss)
$primary-color: #333;
.nav {
background: $primary-color;
ul {
margin: 0;
li {
display: inline-block;
}
}
}
// Sass Syntax (.sass)
$primary-color: #333
.nav
background: $primary-color
ul
margin: 0
li
display: inline-block
Note: SCSS is recommended for most projects due to CSS
compatibility. All valid CSS is valid SCSS.
2. Nesting Rules and Selector Specificity
| Nesting Type | Syntax | Best Practice | Use Case |
|---|---|---|---|
| Basic Nesting | parent { child { } } |
Limit to 3-4 levels deep | Component structure organization |
| Selector Nesting | .parent { .child { } } |
Mirror HTML structure | Maintain readability and scope |
| Pseudo-classes | &:hover, &:focus |
Keep with parent selector | State variations of elements |
| Media Queries | @media { } inside selector |
Nest within component | Component-level responsive design |
| Property Nesting | font: { size: 12px; } |
Use sparingly | Related property groups |
| Deep Nesting Warning | > 4 levels | Refactor or use BEM | Avoid specificity issues |
Example: Proper nesting with depth limit
// Good: 3 levels, clear structure
.card {
padding: 1rem;
&__header {
font-weight: bold;
&:hover {
color: blue;
}
}
@media (min-width: 768px) {
padding: 2rem;
}
}
// Bad: Too deep, overly specific
.nav {
ul {
li {
a {
span { // 5 levels - avoid!
color: red;
}
}
}
}
}
Warning: Deep nesting increases CSS specificity and file size. Keep selectors 3 levels or less when possible.
3. Parent Selector (&) - Pseudo-classes and BEM Patterns
| Usage | Syntax | Output CSS | Common Use Case |
|---|---|---|---|
| Pseudo-class | &:hover |
.btn:hover |
State changes (hover, focus, active) |
| Pseudo-element | &::before |
.box::before |
Generated content |
| Modifier Class | &--large |
.btn--large |
BEM modifier pattern |
| Element Class | &__icon |
.btn__icon |
BEM element pattern |
| Compound Selector | &.active |
.tab.active |
State class combination |
| Parent Reference | .theme-dark & |
.theme-dark .btn |
Context-based styling |
| Attribute Selector | &[disabled] |
.btn[disabled] |
Attribute-based styling |
| Multiple Parents | &-prefix-&-suffix |
Complex string building | Dynamic class generation |
Example: Parent selector (&) patterns
// Pseudo-selectors
.button {
background: blue;
&:hover { background: darkblue; }
&:focus { outline: 2px solid blue; }
&::after { content: '→'; }
}
// BEM Pattern
.card {
border: 1px solid #ccc;
&__title {
font-size: 1.5rem;
}
&--featured {
border-color: gold;
}
}
// Context Selector
.sidebar {
width: 200px;
.mobile & {
width: 100%;
}
}
// Compound Classes
.nav-item {
color: black;
&.active {
color: blue;
font-weight: bold;
}
}
4. Property Nesting and Shorthand Syntax
| Property Group | SCSS Nested Syntax | CSS Output | Use Case |
|---|---|---|---|
| font | font: { size: 16px; weight: bold; } |
font-size: 16px; font-weight: bold; |
Typography properties |
| margin | margin: { top: 10px; bottom: 10px; } |
margin-top: 10px; margin-bottom: 10px; |
Spacing control |
| padding | padding: { left: 20px; right: 20px; } |
padding-left: 20px; padding-right: 20px; |
Inner spacing |
| border | border: { width: 1px; style: solid; } |
border-width: 1px; border-style: solid; |
Border definitions |
| background | background: { color: #fff; size: cover; } |
background-color: #fff; background-size: cover; |
Background layers |
| text | text: { align: center; decoration: underline; } |
text-align: center; text-decoration: underline; |
Text formatting |
Example: Property nesting examples
.box {
font: {
family: Arial, sans-serif;
size: 14px;
weight: 600;
}
margin: {
top: 1rem;
bottom: 1rem;
}
border: {
top: 2px solid blue;
bottom: 1px solid gray;
radius: 4px;
}
}
// Output CSS:
// .box {
// font-family: Arial, sans-serif;
// font-size: 14px;
// font-weight: 600;
// margin-top: 1rem;
// margin-bottom: 1rem;
// border-top: 2px solid blue;
// border-bottom: 1px solid gray;
// border-radius: 4px;
// }
Note: Property nesting is rarely used in practice. Standard CSS
syntax is more common and readable.
5. Comments (// single-line vs /* */ multi-line)
| Comment Type | Syntax | Compiled Output | Best Use |
|---|---|---|---|
| Silent Comment | // comment |
Not included in CSS | Developer notes, TODOs, explanations |
| Loud Comment | /* comment */ |
Included in CSS output | Copyright, section headers, docs |
| Multi-line Silent | // Line 1 |
Not in CSS | Block explanations |
| Multi-line Loud | /* Line 1 |
Preserved in CSS | Documentation blocks |
| Force Comment | /*! Important */ |
Always kept (even compressed) | Licenses, copyright, attribution |
| Interpolation | /* Version: #{$ver} */ |
Variable value inserted | Dynamic documentation |
Example: Comment types and usage
// This comment won't appear in CSS
// Used for developer notes and explanations
/* This comment will appear in CSS output */
/* Use for section headers and user-facing docs */
/*!
* This comment is ALWAYS preserved
* Copyright 2025 - Company Name
*/
$version: '2.1.0';
/* Project Version: #{$version} */
// =========================
// Component: Navigation Bar
// =========================
.navbar {
/* Navigation bar styles */
background: #333;
// TODO: Add responsive breakpoints
// FIXME: z-index conflict with modal
}
// Compressed output will remove /* */ but keep /*! */
6. File Extensions (.scss vs .sass) and @import Conventions
| File Type | Extension | Naming Pattern | Purpose |
|---|---|---|---|
| SCSS Files | .scss |
styles.scss |
Main stylesheet files |
| Sass Files | .sass |
styles.sass |
Indented syntax files |
| Partial Files | .scss |
_partial.scss |
Not compiled directly (imported only) |
| CSS Output | .css |
styles.css |
Compiled output for browser |
| Source Map | .css.map |
styles.css.map |
Debug mapping to source SCSS |
| Import Pattern | @import 'partial'; or @use 'module'; NEW |
||
Example: File structure and import conventions
// File structure:
// styles/
// ├── main.scss (entry point)
// ├── _variables.scss (partial)
// ├── _mixins.scss (partial)
// └── components/
// ├── _button.scss (partial)
// └── _card.scss (partial)
// main.scss (compiled to main.css)
@import 'variables';
@import 'mixins';
@import 'components/button';
@import 'components/card';
// Modern approach with @use
@use 'variables' as vars;
@use 'mixins' as mx;
@use 'components/button';
// Partial file naming: _variables.scss
// Underscore prevents direct compilation
// Import without underscore or extension
@import 'variables'; // finds _variables.scss
| Import Method | Syntax | Status | Notes |
|---|---|---|---|
| @import DEPRECATED | @import 'file'; |
Legacy, being phased out | Global namespace, can cause conflicts |
| @use NEW | @use 'file'; |
Recommended modern approach | Namespaced, prevents conflicts |
| @forward NEW | @forward 'file'; |
Module re-exporting | Create library entry points |
| Index Files | @use 'components'; |
Loads _index.scss | Folder-level imports |
Note: Use underscore prefix for partials to prevent direct
compilation. Modern projects should use
@use instead of @import.
Warning:
@import is deprecated and will be removed from Sass. Migrate to
@use and @forward for future compatibility.