Fallback values help your website look good even if some styles are not supported by the browser.
Fallback values in CSS
property: preferred-value fallback-value;font-family: Arial, 'Helvetica Neue', sans-serif;
color: var(--text-color, #ff0000);
background-image: url('image.webp'), url('image.jpg');
This example shows three types of fallback:
- Font fallback: tries 'NonExistentFont', then Arial, then sans-serif.
- Background color fallback: uses CSS variable if set, otherwise lightblue.
- Background image fallback: tries WebP image first, then JPG.
If you open this in a browser, you will see the text in Arial because the first font does not exist, the background color is light blue if the CSS variable is not set, and the box shows the fallback image if the first image is missing.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Fallback Values Example</title> <style> body { font-family: 'NonExistentFont', Arial, sans-serif; background-color: var(--main-bg-color, lightblue); color: black; padding: 2rem; } .box { width: 10rem; height: 10rem; background-image: url('nonexistent.webp'), url('fallback.jpg'); background-size: cover; border: 2px solid black; margin-top: 1rem; } </style> </head> <body> <h1>Fallback Values in CSS</h1> <p>This text uses a font fallback: if 'NonExistentFont' is not found, it uses Arial.</p> <p>The background color uses a CSS variable with a fallback color.</p> <div class="box" aria-label="Box with fallback background image"></div> </body> </html>
Always list fallback values from most specific to most general.
Use fallback fonts and colors to keep your site readable and attractive on all browsers.
Test your site in different browsers to see how fallbacks work.
Fallback values provide backup styles when the main style is not supported.
They improve website compatibility and user experience.
Use them for fonts, colors, images, and CSS variables.
Practice
Solution
Step 1: Understand fallback values concept
Fallback values are used to ensure styles still apply if the preferred style is unsupported by the browser.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.Final Answer:
To provide backup styles if the main style is not supported -> Option BQuick Check:
Fallback values = backup styles [OK]
- Thinking fallback speeds up loading
- Confusing fallback with animations
- Believing fallback changes HTML
Solution
Step 1: Recall font-family syntax with fallbacks
Fallback fonts are listed separated by commas, from preferred to generic.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.Final Answer:
font-family: 'Open Sans', Arial, sans-serif; -> Option DQuick Check:
Fallback fonts use commas [OK]
- Missing commas between fonts
- Wrong order of fonts
- Using quotes incorrectly
p { color: var(--main-color, blue); }Solution
Step 1: Understand CSS variable fallback syntax
The syntax var(--main-color, blue) means use --main-color if supported, else fallback to blue.Step 2: Consider browser support for CSS variables
If the browser does not support CSS variables, it uses the fallback value blue.Final Answer:
Blue -> Option AQuick Check:
Fallback value used when variable unsupported [OK]
- Assuming variable always works
- Choosing default black color
- Confusing fallback with transparency
div { background-image: url('image.webp', 'image.png'); }Solution
Step 1: Understand fallback for background images
Fallback images are provided by listing multiple url() functions separated by commas.Step 2: Analyze the given code
The code incorrectly puts two URLs inside one url() function, which is invalid syntax.Step 3: Correct usage
Correct syntax: background-image: url('image.webp'), url('image.png');Final Answer:
Fallback images require multiple url() functions separated by commas -> Option AQuick Check:
Multiple url() with commas for fallback [OK]
- Putting multiple URLs inside one url()
- Missing commas between url() functions
- Thinking fallback images not supported
--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?Solution
Step 1: Understand fallback usage for CSS variables
To ensure fallback works for both color and background-color, each var() must include fallback.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.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.Final Answer:
color: var(--primary-color, green); background-color: var(--primary-color, green); -> Option CQuick Check:
Fallback in each var() call needed [OK]
- Adding fallback only once
- Setting fixed color ignoring variable
- Forgetting fallback for background-color
Ready to prove it?
Pass the test and this pattern moves unknown
