Preprocessors like SCSS and PostCSS help write CSS faster and cleaner. They add features like variables and nesting that normal CSS does not have.
Preprocessor support (SCSS, PostCSS) in Svelte
<style lang="scss"> $main-color: #3498db; .button { background-color: $main-color; &:hover { background-color: darken($main-color, 10%); } } </style>
lang="scss" attribute in the <style> tag to enable SCSS preprocessing in Svelte.postcss.config.js.<style lang="scss">
$color: red;
h1 {
color: $color;
}
</style><style lang="postcss"> .box { display: flex; transition: all 0.3s; } </style>
<style lang="scss">
nav {
ul {
list-style: none;
}
}
</style>This Svelte component uses SCSS for styling. The button changes color when clicked, showing how SCSS variables and nesting work together. The class:active directive toggles the active style.
<script> let active = false; </script> <style lang="scss"> $active-color: #27ae60; button { background: #eee; border: none; padding: 1rem 2rem; cursor: pointer; &:hover { background: darken(#eee, 10%); } &.active { background: $active-color; color: white; } } </style> <button class:active={active} on:click={() => active = !active} aria-pressed={active}> {active ? 'Active' : 'Inactive'} </button>
Make sure your build tool (like Vite or Rollup) is configured to handle SCSS or PostCSS.
Use semantic HTML and ARIA attributes for accessibility when styling interactive elements.
Preprocessors add power but keep your CSS readable and maintainable.
Preprocessors let you write better CSS with variables, nesting, and plugins.
In Svelte, add lang="scss" or lang="postcss" to the style tag to enable them.
They help keep styles organized, easier to maintain, and compatible with many browsers.
Practice
Solution
Step 1: Understand Svelte style tag syntax
In Svelte, to enable a CSS preprocessor like SCSS, you add an attribute to the <style> tag.Step 2: Identify the correct attribute for SCSS
The correct attribute islang="scss", which tells Svelte to preprocess styles with SCSS.Final Answer:
lang="scss" -> Option DQuick Check:
Use lang="scss" for SCSS preprocessing [OK]
- Using type="scss" instead of lang
- Adding preprocess attribute which is invalid
- Putting style="scss" which is not recognized
Solution
Step 1: Recall attribute for preprocessors in Svelte
Svelte uses thelangattribute on the <style> tag to specify preprocessors.Step 2: Match the correct syntax for PostCSS
The correct syntax is<style lang="postcss">. Other attributes like type or preprocess are not valid here.Final Answer:
<style lang="postcss"> -> Option AQuick Check:
Use lang="postcss" to enable PostCSS [OK]
- Using postcss attribute without lang
- Using type instead of lang
- Trying preprocess attribute which is invalid
<style lang="scss">
$primary-color: #3498db;
.button {
color: $primary-color;
&:hover {
color: darken($primary-color, 10%);
}
}
</style>What will be the color of the button text on hover?
Solution
Step 1: Understand SCSS variable and nesting
The variable$primary-coloris set to #3498db. The nested hover selector usesdarken($primary-color, 10%).Step 2: Know SCSS darken() function behavior
Thedarken()function reduces brightness by 10%, so the hover color is a darker shade of #3498db.Final Answer:
A color 10% darker than #3498db -> Option BQuick Check:
SCSS darken() darkens color by given percent [OK]
- Thinking darken() is unsupported in Svelte
- Assuming hover color stays the same
- Confusing variable syntax with CSS custom properties
<style lang="scss">
$color: red
.button { color: $color; }
</style>But your app fails to compile. What is the likely cause?
Solution
Step 1: Check SCSS syntax in style block
The variable declaration$color: redis missing a semicolon at the end, which is required in SCSS.Step 2: Understand SCSS syntax rules
SCSS requires semicolons after declarations. Missing it causes a compile error.Final Answer:
Missing semicolon after '$color: red' declaration -> Option AQuick Check:
SCSS needs semicolons after declarations [OK]
- Using 'sass' instead of 'scss' without changing syntax
- Thinking variables are unsupported in Svelte styles
- Assuming SCSS must be imported, not inline
Solution
Step 1: Understand how to enable multiple preprocessors in Svelte
Svelte uses svelte-preprocess configured in svelte.config.js to handle multiple preprocessors like SCSS and PostCSS together.Step 2: Identify correct configuration method
You must configure svelte-preprocess with bothscssandpostcssoptions in svelte.config.js. Adding multiple lang attributes or importing PostCSS in script won't work.Final Answer:
Configure svelte.config.js with svelte-preprocess using both scss and postcss options -> Option CQuick Check:
Use svelte-preprocess config for multiple preprocessors [OK]
- Trying to add multiple lang attributes in style tag
- Importing PostCSS in script block instead of config
- Using separate style tags without lang attributes
Ready to prove it?
Pass the test and this pattern moves unknown
