Bird
Raised Fist0
Tailwindmarkup~20 mins

CSS variables with Tailwind - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CSS Variables with Tailwind Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding CSS Variables in Tailwind
Which statement correctly describes how CSS variables can be used with Tailwind CSS?
ACSS variables can be defined in a <style> block or external CSS and then referenced inside Tailwind utility classes using var(--variable-name).
BTailwind automatically converts all CSS variables into fixed colors during build time, so you cannot use var() inside Tailwind classes.
CCSS variables cannot be used with Tailwind CSS because Tailwind only supports fixed values.
DCSS variables must be declared inside Tailwind config file to be used in utility classes.
Attempts:
2 left
💡 Hint
Think about how CSS variables work in normal CSS and how Tailwind utilities can reference them.
📝 Syntax
intermediate
2:00remaining
Using CSS Variables in Tailwind Utility Classes
Which Tailwind class correctly applies a background color using a CSS variable named --main-bg?
Abg--main-bg
Bbg-var(--main-bg)
Cbg-[var(--main-bg)]
Dbg(var(--main-bg))
Attempts:
2 left
💡 Hint
Tailwind uses square brackets to allow arbitrary values.
❓ rendering
advanced
2:00remaining
Visual Result of CSS Variable with Tailwind
Given the CSS and HTML below, what color will the box's background be when rendered in the browser?
Tailwind
/* CSS */
:root {
  --box-color: #4ade80;
}

/* HTML */
<div class="w-40 h-40 bg-[var(--box-color)]"></div>
AA green box with a background color #4ade80
BA box with no background color (transparent)
CA box with a red background color
DThe box will not render due to invalid CSS
Attempts:
2 left
💡 Hint
Check the CSS variable value and how it is used in the Tailwind class.
❓ selector
advanced
2:00remaining
Targeting Elements with CSS Variables and Tailwind
You want to create a button that changes its text color based on a CSS variable --btn-text. Which Tailwind class correctly applies this text color?
Atext--btn-text
Btext-[var(--btn-text)]
Ctext-var(--btn-text)
Dtext(var(--btn-text))
Attempts:
2 left
💡 Hint
Remember how Tailwind handles arbitrary values with square brackets.
❓ accessibility
expert
3:00remaining
Ensuring Accessibility with CSS Variables and Tailwind
You use a CSS variable --primary-color for background and text colors in Tailwind. What is the best practice to ensure good accessibility (color contrast) for all users?
ASet the CSS variable colors only for light mode and ignore dark mode contrast.
BUse any colors you like in CSS variables because Tailwind automatically adjusts contrast.
CAvoid using CSS variables for colors and only use Tailwind's default color palette for accessibility.
DDefine the CSS variable with colors that meet WCAG contrast standards and test with tools; update variables if needed.
Attempts:
2 left
💡 Hint
Accessibility means good contrast and testing with real tools.

Practice

(1/5)
1. What is the main benefit of using CSS variables with Tailwind CSS?
easy
A. They make your CSS files larger and slower to load.
B. They automatically generate new classes for every color.
C. They replace the need for Tailwind utility classes.
D. They allow you to reuse values like colors and spacing easily.

Solution

  1. Step 1: Understand CSS variables purpose

    CSS variables store values like colors or spacing to reuse them easily.
  2. Step 2: Connect with Tailwind usage

    Tailwind supports CSS variables to keep design consistent and easy to update.
  3. Final Answer:

    They allow you to reuse values like colors and spacing easily. -> Option D
  4. Quick Check:

    CSS variables = reuse values [OK]
Hint: CSS variables store reusable values for consistency [OK]
Common Mistakes:
  • Thinking CSS variables replace Tailwind classes
  • Believing CSS variables slow down CSS loading
  • Assuming CSS variables auto-generate classes
2. Which is the correct way to use a CSS variable for color in Tailwind's arbitrary value syntax?
easy
A. bg-[var(--main-color)]
B. bg-var(--main-color)
C. bg-[--main-color]
D. bg-[var(main-color)]

Solution

  1. Step 1: Recall Tailwind arbitrary value syntax

    Tailwind uses square brackets for arbitrary values, e.g., bg-[value].
  2. Step 2: Use correct CSS variable syntax inside brackets

    CSS variables use var(--variable-name), so bg-[var(--main-color)] is correct.
  3. Final Answer:

    bg-[var(--main-color)] -> Option A
  4. Quick Check:

    Arbitrary value with var() = bg-[var(--main-color)] [OK]
Hint: Use bg-[var(--name)] for CSS variables in Tailwind [OK]
Common Mistakes:
  • Omitting var() function
  • Missing square brackets around the value
  • Using incorrect variable syntax like var(main-color)
3. Given this CSS: :root { --btn-color: #4ade80; } and this Tailwind class: class="bg-[var(--btn-color)] p-4 rounded", what color will the button background be?
medium
A. Default Tailwind blue color.
B. No background color applied.
C. A bright green color (#4ade80).
D. An error will occur and no style applied.

Solution

  1. Step 1: Identify CSS variable value

    The variable --btn-color is set to #4ade80, a bright green color.
  2. Step 2: Understand Tailwind usage

    The class bg-[var(--btn-color)] uses that variable for background color.
  3. Final Answer:

    A bright green color (#4ade80). -> Option C
  4. Quick Check:

    CSS var used correctly = green background [OK]
Hint: Check CSS variable value to predict Tailwind color output [OK]
Common Mistakes:
  • Assuming Tailwind default color overrides variable
  • Thinking variable is ignored without explicit Tailwind color
  • Believing syntax causes error
4. What is wrong with this Tailwind class if the CSS variable is defined as --text-color: #333;?
class="text-[var(text-color)] font-bold"
medium
A. Missing double dash before variable name inside var().
B. Square brackets are not allowed in Tailwind classes.
C. CSS variables cannot be used with text color in Tailwind.
D. font-bold conflicts with text color variable.

Solution

  1. Step 1: Check CSS variable syntax

    CSS variables require two dashes: --text-color, so var(--text-color) is correct.
  2. Step 2: Identify error in class

    The class uses var(text-color) missing the double dash, causing invalid variable reference.
  3. Final Answer:

    Missing double dash before variable name inside var(). -> Option A
  4. Quick Check:

    CSS var syntax needs -- before name [OK]
Hint: Always use -- before variable name inside var() [OK]
Common Mistakes:
  • Omitting -- in CSS variable name
  • Thinking font-bold affects color variables
  • Believing square brackets are invalid in Tailwind
5. You want to create a reusable button style in Tailwind that uses a CSS variable for background color and changes on hover. Which code snippet correctly applies this using CSS variables and Tailwind?
hard
A. Click me
B. Click me
C. Click me
D. Click me

Solution

  1. Step 1: Use correct CSS variable syntax in Tailwind

    Tailwind requires arbitrary values in brackets with var(--variable), so bg-[var(--btn-bg)] is correct.
  2. Step 2: Apply hover state correctly

    Hover uses hover:bg-[var(--btn-hover)] to change background on hover properly.
  3. Final Answer:

    <button class='bg-[var(--btn-bg)] hover:bg-[var(--btn-hover)] text-white px-4 py-2 rounded'>Click me</button> -> Option B
  4. Quick Check:

    Correct var() syntax + hover state = Click me [OK]
Hint: Use bg-[var(--name)] and hover:bg-[var(--name)] for hover colors [OK]
Common Mistakes:
  • Omitting var() or -- in variable names
  • Using bg-var() without brackets
  • Incorrect hover syntax without hover: prefix
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard