Bird
Raised Fist0
CSSmarkup~10 mins

Fallback values in CSS - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a fallback font before the main font.

CSS
body { font-family: [1], Arial, sans-serif; }
Drag options to blanks, or click blank then click option'
Abackground
Bcolor
CHelvetica
Dmargin
Attempts:
3 left
💡 Hint
Common Mistakes
Using CSS properties like color or margin instead of a font name.
Leaving the main font blank.
2fill in blank
medium

Complete the code to provide a fallback background color.

CSS
div { background: [1] linear-gradient(to right, #00ff00); }
Drag options to blanks, or click blank then click option'
Ared
Bborder
Cpadding
Dfont-size
Attempts:
3 left
💡 Hint
Common Mistakes
Using CSS properties instead of color values.
Using invalid color names.
3fill in blank
hard

Fix the error in the fallback font list.

CSS
p { font-family: [1], 'Times New Roman', serif; }
Drag options to blanks, or click blank then click option'
AArial
Bfont-weight
Ccolor
Dpadding
Attempts:
3 left
💡 Hint
Common Mistakes
Using CSS properties instead of font names.
Leaving the first font blank.
4fill in blank
hard

Fill both blanks to set a fallback font and a fallback background color.

CSS
h1 { font-family: [1], Verdana, sans-serif; background: [2] linear-gradient(to right, #0000ff); }
Drag options to blanks, or click blank then click option'
AGeorgia
Bred
Cblue
Dpadding
Attempts:
3 left
💡 Hint
Common Mistakes
Using CSS properties instead of font names or colors.
Mixing up the order of font and color values.
5fill in blank
hard

Fill all three blanks to set fallback fonts and a fallback background color with a gradient.

CSS
section { font-family: [1], 'Courier New', monospace; background: [2] linear-gradient(to right, #ff0000, #0000ff) [3]; }
Drag options to blanks, or click blank then click option'
ATahoma
Byellow
Cno-repeat
Dpadding
Attempts:
3 left
💡 Hint
Common Mistakes
Using CSS properties instead of font names or colors.
Using invalid background styles.

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