📖 Scenario: You are building a simple Svelte component that shows a colored box with text. You want to style the box using CSS-in-JS patterns, meaning the styles are written inside the component file using JavaScript variables and applied dynamically.
🎯 Goal: Create a Svelte component that uses CSS-in-JS patterns to style a box with dynamic colors and padding. The styles should be defined as JavaScript variables and applied inline or via style bindings.
📋 What You'll Learn
Create a Svelte component with a div element representing a box
Define JavaScript variables for backgroundColor and padding
Apply these style variables to the div using Svelte's style binding
Add a text inside the box that says 'Styled Box'
💡 Why This Matters
🌍 Real World
CSS-in-JS patterns help keep styles close to components, making it easier to manage dynamic styling in modern web apps.
💼 Career
Understanding CSS-in-JS with Svelte is useful for frontend developers building interactive, component-based user interfaces.
Progress0 / 4 steps
1
Set up the box element
Create a Svelte component with a div element that contains the text Styled Box.
Svelte
Hint
Start by writing a simple <div> with the text inside it.
2
Define style variables
Add two JavaScript variables inside a <script> tag: backgroundColor set to "lightblue" and padding set to "1rem".
Svelte
Hint
Use let to declare variables inside the <script> tag.
3
Apply styles using CSS-in-JS pattern
Use Svelte's style binding to apply the backgroundColor and padding variables as inline styles on the div. Use the syntax style="background-color: {backgroundColor}; padding: {padding};".
Svelte
Hint
Use curly braces {} inside the style attribute to insert JavaScript variables.
4
Make the styles reactive
Add a button below the div that changes backgroundColor to "lightcoral" when clicked. Use Svelte's on:click event binding with a function that sets backgroundColor = "lightcoral".
Svelte
Hint
Define a function inside <script> and bind it to the button's on:click event.
Practice
(1/5)
1. What is the main advantage of using CSS-in-JS patterns in Svelte components?
easy
A. It forces all styles to be global across the app.
B. It requires importing external CSS files for every component.
C. It disables scoped styles by default.
D. Styles stay close to the component's markup and logic, improving maintainability.
Solution
Step 1: Understand CSS-in-JS purpose in Svelte
CSS-in-JS means writing styles inside the component file, close to markup and logic.
Step 2: Identify the benefit of this approach
This closeness improves maintainability by keeping related code together and avoiding global style conflicts.
Final Answer:
Styles stay close to the component's markup and logic, improving maintainability. -> Option D
2. Which of the following is the correct way to write scoped CSS inside a Svelte component?
easy
A.
B. h1 { color: red; }
C.
D.
Solution
Step 1: Identify correct tag for scoped styles in Svelte
Svelte uses the <style> tag inside components for scoped CSS by default.
Step 2: Eliminate incorrect tags
<style global> makes styles global, <css> and <script> are invalid for CSS.
Final Answer:
<style> h1 { color: red; } </style> -> Option C
Quick Check:
Scoped CSS uses <style> tag [OK]
Hint: Use <style> for scoped CSS, not
Common Mistakes:
Using
Writing CSS inside <script> tags
Using non-existent <css> tags
3. Consider this Svelte component snippet:
<script>
let color = 'blue';
</script>
<div style="color: {color};">Hello</div>
What color will the text "Hello" appear as when rendered?
medium
A. Blue
B. Green
C. Black
D. Red
Solution
Step 1: Check the variable value
The variable 'color' is set to 'blue' in the script.
Step 2: Understand inline style binding
The div's style attribute uses {color}, so it applies 'color: blue;'.
Final Answer:
Blue -> Option A
Quick Check:
Inline style uses variable 'color' = blue [OK]
Hint: Inline style uses JS variable value directly [OK]
Common Mistakes:
Assuming default black color instead of variable
Confusing CSS property with variable name
Forgetting to use curly braces for JS in style
4. Identify the error in this Svelte component CSS-in-JS snippet:
<script>
let bg = 'yellow';
</script>
<div style="background-color: bg;">Box</div>
medium
A. Variable 'bg' should be declared with const, not let.
B. Missing curly braces around 'bg' in style attribute.
C. CSS property 'background-color' is invalid in Svelte.
D. Style attribute must be written inside <style> tag.
Solution
Step 1: Check how JS variables are used in inline styles
JS variables must be wrapped in curly braces inside style attributes to interpolate values.
Step 2: Identify the missing curly braces
The style uses "background-color: bg;" but should be "background-color: {bg};" to apply the variable.
Final Answer:
Missing curly braces around 'bg' in style attribute. -> Option B
Quick Check:
Use {variable} inside style attribute [OK]
Hint: Wrap JS variables in {} inside style attributes [OK]
Common Mistakes:
Forgetting curly braces for JS in style
Thinking CSS properties are invalid in Svelte
Believing style must be only in <style> tag
5. You want to create a Svelte component that changes text color dynamically based on a prop isError. Which approach correctly applies CSS-in-JS to achieve this?