Bird
Raised Fist0
Tailwindmarkup~10 mins

Media-based dark mode strategy in Tailwind - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable dark mode based on user's system preference.

Tailwind
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Dark Mode Example</title>
  <script>
    tailwind.config = {
      darkMode: '[1]'
    }
  </script>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-white dark:bg-gray-900 text-black dark:text-white">
  <h1 class="text-2xl font-bold">Hello, World!</h1>
</body>
</html>
Drag options to blanks, or click blank then click option'
Amedia
Bclass
Cmanual
Dauto
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'media' will require manual toggling.
Using 'auto' or 'manual' are not valid Tailwind dark mode strategies.
2fill in blank
medium

Complete the class attribute to apply dark mode background color.

Tailwind
<div class="bg-white [1]:bg-gray-800 p-4">
  <p>This box changes color in dark mode.</p>
</div>
Drag options to blanks, or click blank then click option'
Adark
Blight
Cmedia
Dmode
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'light:' or 'media:' instead of 'dark:' will not apply dark mode styles.
Forgetting the colon after the prefix.
3fill in blank
hard

Fix the error in the Tailwind config to enable media-based dark mode.

Tailwind
module.exports = {
  darkMode: '[1]',
  content: ['./src/**/*.{html,js}'],
  theme: {
    extend: {},
  },
  plugins: [],
}
Drag options to blanks, or click blank then click option'
Aclass
Bmedia
CMedia
DMEDIA
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase or capitalized versions causes Tailwind to ignore the setting.
Using 'class' instead of 'media' changes the dark mode strategy.
4fill in blank
hard

Fill both blanks to create a responsive dark mode button with hover effect.

Tailwind
<button class="bg-white [1]:bg-gray-700 text-black [2]:text-white px-4 py-2 rounded">
  Click me
</button>
Drag options to blanks, or click blank then click option'
Adark
Bhover
Cfocus
Dactive
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'focus' or 'active' instead of 'hover' for mouse hover effect.
Using 'class' or 'media' as prefixes in class names.
5fill in blank
hard

Fill all three blanks to create a dark mode card with shadow and rounded corners.

Tailwind
<div class="bg-white [1]:bg-gray-900 [2]shadow-lg [3]rounded-lg p-6">
  <h2 class="text-xl font-semibold">Card Title</h2>
  <p>This card adapts to dark mode.</p>
</div>
Drag options to blanks, or click blank then click option'
Adark
Bhover
Cfocus
Dactive
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'focus' with 'active' or 'hover'.
Using 'media' or 'class' as prefixes in class names.

Practice

(1/5)
1. What does setting darkMode: 'media' in Tailwind CSS configuration do?
easy
A. It enables dark mode styles based on the user's system preference.
B. It forces dark mode on all users regardless of preference.
C. It disables dark mode completely.
D. It requires manual toggling of dark mode via JavaScript.

Solution

  1. Step 1: Understand Tailwind dark mode options

    Tailwind can detect user preference automatically with darkMode: 'media'.
  2. Step 2: Effect of darkMode: 'media'

    This setting applies dark styles only if the user's system prefers dark mode.
  3. Final Answer:

    It enables dark mode styles based on the user's system preference. -> Option A
  4. Quick Check:

    darkMode: 'media' = automatic user preference [OK]
Hint: Remember 'media' means system preference triggers dark mode [OK]
Common Mistakes:
  • Thinking it forces dark mode for all users
  • Confusing 'media' with manual toggle
  • Assuming it disables dark mode
2. Which of the following is the correct way to apply a background color only in dark mode using Tailwind with media strategy?
easy
A. bg-gray-800 dark:bg-gray-200
B. bg-gray-800 bg-dark:bg-gray-200
C. dark:bg-gray-800 bg-gray-200
D. bg-gray-800 darkmode:bg-gray-200

Solution

  1. Step 1: Understand Tailwind dark mode prefix

    The correct prefix for dark mode styles is dark:, placed before the class.
  2. Step 2: Correct order for background colors

    Normal background first, then dark:bg-* overrides in dark mode.
  3. Final Answer:

    dark:bg-gray-800 bg-gray-200 -> Option C
  4. Quick Check:

    Use dark: prefix before class [OK]
Hint: Use dark: prefix before classes for dark mode [OK]
Common Mistakes:
  • Using wrong prefix like 'darkmode:'
  • Placing dark class after normal class incorrectly
  • Writing 'bg-dark:' instead of 'dark:bg-'
3. Given this Tailwind config snippet:
{ darkMode: 'media' }
and this HTML:
<div class="bg-white dark:bg-black text-black dark:text-white">Hello</div>
What background and text colors will appear if the user's system prefers dark mode?
medium
A. Background white, text black
B. Background black, text white
C. Background white, text white
D. Background black, text black

Solution

  1. Step 1: Check dark mode config

    With darkMode: 'media', dark styles apply if system prefers dark mode.
  2. Step 2: Analyze classes in dark mode

    dark:bg-black sets background black, dark:text-white sets text white in dark mode.
  3. Final Answer:

    Background black, text white -> Option B
  4. Quick Check:

    dark:bg-black + dark:text-white = black bg, white text [OK]
Hint: Dark mode classes override normal ones when system prefers dark [OK]
Common Mistakes:
  • Ignoring dark mode overrides
  • Mixing text and background colors
  • Assuming normal styles always show
4. You wrote this Tailwind CSS config:
{ darkMode: 'media' }
and your HTML:
<div class="bg-white darkmode:bg-black">Test</div>
Why does the dark mode background not apply when system prefers dark mode?
medium
A. Because the prefix should be dark:, not darkmode:
B. Because darkMode must be set to 'class' instead of 'media'
C. Because bg-white overrides dark background always
D. Because Tailwind does not support dark mode with media queries

Solution

  1. Step 1: Check dark mode prefix usage

    The correct prefix for dark mode classes is dark:, not darkmode:.
  2. Step 2: Effect of wrong prefix

    Using darkmode:bg-black is invalid and ignored by Tailwind.
  3. Final Answer:

    Because the prefix should be dark:, not darkmode: -> Option A
  4. Quick Check:

    Use exact dark: prefix for dark mode [OK]
Hint: Use exactly dark: prefix, no variations [OK]
Common Mistakes:
  • Typing 'darkmode:' instead of 'dark:'
  • Changing config to 'class' unnecessarily
  • Assuming normal classes override dark mode
5. You want to create a responsive card that changes background color in dark mode only on screens wider than 768px using Tailwind with media-based dark mode. Which class combination achieves this?
hard
A. bg-white md:bg-gray-200 dark:bg-gray-800
B. bg-white dark:md:bg-gray-800
C. bg-white dark:bg-gray-800 md:bg-gray-200
D. bg-white md:dark:bg-gray-800

Solution

  1. Step 1: Understand responsive and dark mode prefixes

    Tailwind applies prefixes from left to right: md:dark: means dark mode styles apply only on medium screens and up.
  2. Step 2: Analyze each option

    bg-white md:dark:bg-gray-800 uses md:dark:bg-gray-800 which applies dark background only on screens ≥768px and in dark mode.
  3. Final Answer:

    bg-white md:dark:bg-gray-800 -> Option D
  4. Quick Check:

    Use md:dark: to combine media query and dark mode [OK]
Hint: Chain prefixes: screen size first, then dark mode [OK]
Common Mistakes:
  • Reversing prefix order like 'dark:md:'
  • Applying dark mode without screen size condition
  • Using unrelated classes that override styles
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