Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
When to use SASS vs CSS-in-JS
📖 Scenario: You are working on a web project where you need to style components efficiently. You want to understand when to use SASS, a CSS preprocessor, and when to use CSS-in-JS, a styling method inside JavaScript.
🎯 Goal: Learn the differences between SASS and CSS-in-JS and create a simple example using SASS variables and nesting, then add a CSS-in-JS style block in JavaScript to see how both approaches work.
📋 What You'll Learn
Create a SASS file with variables and nested styles
Create a JavaScript file with a CSS-in-JS style object
Understand when to use SASS for global styles and CSS-in-JS for component-level styles
Use semantic HTML to apply styles
Ensure styles are responsive and accessible
💡 Why This Matters
🌍 Real World
Web developers often choose between SASS and CSS-in-JS depending on project needs. Understanding when to use each helps build maintainable and scalable styles.
💼 Career
Knowing both SASS and CSS-in-JS is valuable for frontend developer roles, as many companies use one or both for styling React or other component-based frameworks.
Progress0 / 4 steps
1
Create a SASS file with variables and nested styles
Create a SASS file named styles.scss. Define a variable $primary-color with the value #3498db. Then create a style for the body element that sets the background color to $primary-color. Inside body, nest a style for h1 that sets the font size to 2rem and color to white.
SASS
Hint
Use SASS variables with $ and nest the h1 style inside the body style block.
2
Create a JavaScript file with a CSS-in-JS style object
Create a JavaScript file named styles.js. Define a constant object called buttonStyle with properties: backgroundColor set to '#e74c3c', color set to 'white', padding set to '0.5rem 1rem', and borderRadius set to '0.25rem'.
SASS
Hint
Use a JavaScript object with camelCase property names for CSS-in-JS styles.
3
Explain when to use SASS for global styles
Add a comment in the JavaScript file explaining that SASS is best used for global styles, reusable variables, and complex nested selectors that apply across the whole website.
SASS
Hint
Write a clear comment starting with // explaining SASS usage for global styles.
4
Explain when to use CSS-in-JS for component-level styles
Add a comment in the JavaScript file explaining that CSS-in-JS is great for styling individual components, dynamic styles based on state, and keeping styles close to the component logic.
SASS
Hint
Write a clear comment starting with // explaining CSS-in-JS usage for component-level styles.
Practice
(1/5)
1. Which situation is best suited for using SASS instead of CSS-in-JS?
easy
A. When you want to avoid using any CSS preprocessors or JavaScript.
B. When you want styles tightly coupled with JavaScript components for dynamic styling.
C. When you want to write inline styles directly inside HTML tags.
D. When you want to write styles in separate files with powerful CSS features like variables and nesting.
Solution
Step 1: Understand SASS purpose
SASS is a CSS preprocessor that adds features like variables, nesting, and mixins to CSS, usually in separate style files.
Step 2: Compare with CSS-in-JS use case
CSS-in-JS is best when styles need to be tightly integrated with JavaScript components, often for dynamic styling.
Final Answer:
When you want to write styles in separate files with powerful CSS features like variables and nesting. -> Option D
Confusing CSS-in-JS as better for all styling needs
Thinking SASS is only for inline styles
Assuming CSS-in-JS cannot use variables
2. Which of the following is the correct way to import a SASS file into another SASS file?
easy
A. @import 'styles.css';
B. @import 'variables.scss';
C. import 'variables.scss';
D. require('variables.scss');
Solution
Step 1: Recall SASS import syntax
SASS uses @import 'filename.scss'; to include other SASS files.
Step 2: Check options for correct syntax
@import 'variables.scss'; uses @import 'variables.scss'; which is correct. @import 'styles.css'; imports a CSS file, which is allowed but not typical for SASS partials. Options C and D use JavaScript syntax, which is incorrect in SASS.
Final Answer:
@import 'variables.scss'; -> Option B
Quick Check:
SASS import uses @import 'file.scss' = A [OK]
Hint: SASS imports use @import with quotes and .scss extension [OK]
But the hover style is not working. What is the likely problem?
medium
A. CSS-in-JS does not support pseudo-classes like :hover.
B. The color values must be variables, not strings.
C. The syntax for nested selectors in CSS-in-JS is incorrect; it should use a string key like ':hover' instead of '&:hover'.
D. The styles object must be converted to a CSS file manually.
Solution
Step 1: Understand CSS-in-JS pseudo-class syntax
In many CSS-in-JS libraries, nested selectors use the key ':hover' without the ampersand (&).
Step 2: Identify syntax error
The code uses '&:hover' which is valid in SASS but often incorrect in CSS-in-JS, causing hover styles to fail.
Final Answer:
The syntax for nested selectors in CSS-in-JS is incorrect; it should use a string key like ':hover' instead of '&:hover'. -> Option C
Quick Check:
CSS-in-JS pseudo-classes use ':hover' key, not '&:hover' [OK]
Hint: Use ':hover' key in CSS-in-JS, not '&:hover' [OK]
Common Mistakes:
Using SASS syntax in CSS-in-JS
Thinking CSS-in-JS can't do hover
Forgetting to apply styles via className
5. You have a React project where components need dynamic styles based on props, but also want to share common styles across many components in separate files. Which approach best fits this need?
hard
A. Combine SASS for shared styles in separate files and CSS-in-JS for dynamic styles inside components.
B. Use only CSS-in-JS to keep all styles inside components with no external files.
C. Write all styles inline in HTML style attributes.
D. Use only SASS with separate .scss files and import them everywhere.
Solution
Step 1: Analyze project needs
The project needs shared common styles in separate files and dynamic styles based on props inside components.
Step 2: Match approaches to needs
SASS is great for shared styles in separate files. CSS-in-JS excels at dynamic styling inside components.
Step 3: Combine approaches
Using both allows shared styles in SASS files and dynamic styles with CSS-in-JS, fitting both requirements.
Final Answer:
Combine SASS for shared styles in separate files and CSS-in-JS for dynamic styles inside components. -> Option A