📖 Scenario: You are building a simple webpage using Svelte. You want to apply some styles that affect the entire page, like setting the background color and font for all text.
🎯 Goal: Create global CSS styles in a Svelte project that set the background color to light gray and the font family to Arial for the whole page.
📋 What You'll Learn
Create a Svelte component with a <style> block
Use the :global selector to apply styles globally
Set the body background color to #f0f0f0
Set the body font family to Arial, sans-serif
Add a simple <h1> heading to see the styles applied
💡 Why This Matters
🌍 Real World
Global styles let you set consistent look and feel for your whole website or app, like fonts and background colors.
💼 Career
Knowing how to apply global styles in Svelte is important for frontend developers to maintain consistent design across components.
Progress0 / 4 steps
1
Create the Svelte component structure
Create a Svelte component with an <h1> heading that says Welcome to Global Styles.
Svelte
Hint
Just write the heading inside the component's markup.
2
Add a style block for global styles
Add a <style> block below the heading. Inside it, start a global style block using :global(body).
Svelte
Hint
Use :global(body) to write styles that affect the body element globally.
3
Set the body background color globally
Inside the :global(body) block, set the background-color to #f0f0f0.
The :global() selector inside a normal <style> tag applies styles globally to the selector inside parentheses.
Step 2: Verify CSS correctness
The CSS property 'background' with value '#eee' is valid and applies a light gray background.
Final Answer:
No error, code is correct -> Option B
Quick Check:
:global() selector works inside <style> [OK]
Hint: Use :global(selector) inside <style> for global CSS [OK]
Common Mistakes:
Thinking
Misunderstanding :global() syntax
Assuming CSS property is wrong
5. You want to apply a global font style to your entire Svelte app but also have a component with a local style that overrides the font color. Which approach correctly achieves this?
hard
A. Use only normal <style> for all styles, no global styles
B. Use only
C. Use
D. Use
Solution
Step 1: Apply global font-family
Use <style global> to set font-family globally so all components inherit it.
Step 2: Override font color locally
Use normal <style> in the component to set font color locally, which overrides global styles.
Final Answer:
Use <style global> for font-family and normal <style> for local font color override -> Option C
Quick Check:
Global styles + local overrides = Use
Hint: Global styles for base, local styles for overrides [OK]
Common Mistakes:
Trying to override global styles inside
Using
Not separating global and local styles properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.