Bird
Raised Fist0
CSSmarkup~20 mins

Fallback values in CSS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
CSS Fallback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
1:30remaining
What color will the text be?
Given the CSS below, what color will the text inside the <p> tag appear as if the variable --main-color is NOT defined?
CSS
p {
  color: var(--main-color, blue);
}
AThe text will be red.
BThe text will be black (default browser color).
CThe text will be transparent.
DThe text will be blue.
Attempts:
2 left
💡 Hint
Look at the second value inside the var() function.
🧠 Conceptual
intermediate
1:30remaining
Why use fallback values in CSS variables?
Which of the following is the main reason to use fallback values with CSS variables?
ATo make the CSS file smaller.
BTo provide a default style if the variable is not set or supported.
CTo speed up the browser rendering process.
DTo disable the variable feature in older browsers.
Attempts:
2 left
💡 Hint
Think about what happens if a variable is missing.
rendering
advanced
2:00remaining
What background color will the box have?
Consider this CSS snippet. What background color will the <div> have if the browser supports CSS variables but the variable --bg-color is NOT defined anywhere?
CSS
div {
  background-color: var(--bg-color, lightgray);
  width: 100px;
  height: 100px;
}
AThe background color will be lightgray.
BThe background color will be the value of --bg-color.
CThe background color will be black.
DThe background color will be transparent.
Attempts:
2 left
💡 Hint
If the variable is missing, what does the fallback do?
selector
advanced
1:30remaining
Which CSS rule applies fallback correctly?
Which of the following CSS rules correctly uses a fallback value for the font size variable --font-size?
Afont-size: var(--font-size, 16px);
Bfont-size: var(--font-size) || 16px;
Cfont-size: var(--font-size); fallback: 16px;
Dfont-size: var(--font-size 16px);
Attempts:
2 left
💡 Hint
Check the syntax of the var() function.
accessibility
expert
2:30remaining
How do fallback colors affect accessibility?
If a CSS variable for text color is missing and the fallback color has poor contrast with the background, what is the best practice to maintain accessibility?
ARely on browser defaults for color if the variable is missing.
BUse transparent fallback colors to avoid conflicts.
CChoose fallback colors with good contrast to ensure readability.
DUse the same color for fallback as the background color.
Attempts:
2 left
💡 Hint
Think about how color contrast affects people with vision difficulties.

Practice

(1/5)
1. What is the main purpose of using fallback values in CSS?
easy
A. To make the website load faster
B. To provide backup styles if the main style is not supported
C. To add animations to elements
D. To change the HTML structure dynamically

Solution

  1. Step 1: Understand fallback values concept

    Fallback values are used to ensure styles still apply if the preferred style is unsupported by the browser.
  2. Step 2: Identify the purpose from options

    Only To provide backup styles if the main style is not supported correctly describes fallback values as backup styles.
  3. Final Answer:

    To provide backup styles if the main style is not supported -> Option B
  4. Quick Check:

    Fallback values = backup styles [OK]
Hint: Fallback means backup style if main fails [OK]
Common Mistakes:
  • Thinking fallback speeds up loading
  • Confusing fallback with animations
  • Believing fallback changes HTML
2. Which of the following is the correct way to provide fallback fonts in CSS?
easy
A. font-family: sans-serif 'Open Sans';
B. font-family: 'Open Sans';
C. font-family: Arial 'Open Sans';
D. font-family: 'Open Sans', Arial, sans-serif;

Solution

  1. Step 1: Recall font-family syntax with fallbacks

    Fallback fonts are listed separated by commas, from preferred to generic.
  2. Step 2: Check each option's syntax

    font-family: 'Open Sans', Arial, sans-serif; correctly lists 'Open Sans', then Arial, then generic sans-serif with commas.
  3. Final Answer:

    font-family: 'Open Sans', Arial, sans-serif; -> Option D
  4. Quick Check:

    Fallback fonts use commas [OK]
Hint: Separate fallback fonts with commas [OK]
Common Mistakes:
  • Missing commas between fonts
  • Wrong order of fonts
  • Using quotes incorrectly
3. What color will the text be if the browser does not support CSS variables in this code?
p { color: var(--main-color, blue); }
medium
A. Blue
B. The color defined by --main-color variable
C. Black (default color)
D. Transparent

Solution

  1. Step 1: Understand CSS variable fallback syntax

    The syntax var(--main-color, blue) means use --main-color if supported, else fallback to blue.
  2. Step 2: Consider browser support for CSS variables

    If the browser does not support CSS variables, it uses the fallback value blue.
  3. Final Answer:

    Blue -> Option A
  4. Quick Check:

    Fallback value used when variable unsupported [OK]
Hint: Fallback after comma used if variable unsupported [OK]
Common Mistakes:
  • Assuming variable always works
  • Choosing default black color
  • Confusing fallback with transparency
4. Identify the error in this CSS fallback usage:
div { background-image: url('image.webp', 'image.png'); }
medium
A. Fallback images require multiple url() functions separated by commas
B. Only one URL is allowed inside url() function
C. The URLs should be separated by commas as shown
D. Fallback images are not supported in CSS

Solution

  1. Step 1: Understand fallback for background images

    Fallback images are provided by listing multiple url() functions separated by commas.
  2. Step 2: Analyze the given code

    The code incorrectly puts two URLs inside one url() function, which is invalid syntax.
  3. Step 3: Correct usage

    Correct syntax: background-image: url('image.webp'), url('image.png');
  4. Final Answer:

    Fallback images require multiple url() functions separated by commas -> Option A
  5. Quick Check:

    Multiple url() with commas for fallback [OK]
Hint: Use separate url() calls for fallback images [OK]
Common Mistakes:
  • Putting multiple URLs inside one url()
  • Missing commas between url() functions
  • Thinking fallback images not supported
5. You want to set a CSS variable --primary-color with a fallback to green if the variable is not defined. Which CSS rule correctly applies this fallback to the text color and background-color?
hard
A. color: var(--primary-color, green); background-color: var(--primary-color);
B. color: var(--primary-color); background-color: green;
C. color: var(--primary-color, green); background-color: var(--primary-color, green);
D. color: green; background-color: var(--primary-color, green);

Solution

  1. Step 1: Understand fallback usage for CSS variables

    To ensure fallback works for both color and background-color, each var() must include fallback.
  2. Step 2: Analyze each option

    color: var(--primary-color, green); background-color: var(--primary-color, green); uses var(--primary-color, green) for both properties, ensuring fallback if variable undefined.
  3. Step 3: Why others are incorrect

    color: var(--primary-color, green); background-color: var(--primary-color); misses fallback for background-color; B misses fallback for color; C sets color fixed to green ignoring variable.
  4. Final Answer:

    color: var(--primary-color, green); background-color: var(--primary-color, green); -> Option C
  5. Quick Check:

    Fallback in each var() call needed [OK]
Hint: Add fallback inside every var() call [OK]
Common Mistakes:
  • Adding fallback only once
  • Setting fixed color ignoring variable
  • Forgetting fallback for background-color