Bird
Raised Fist0
Tailwindmarkup~20 mins

Creating custom utilities with addUtilities in Tailwind - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Tailwind Utilities Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
What is the correct syntax to add a custom utility for a 3rem margin using addUtilities?
You want to create a custom utility class .m-12 that applies margin: 3rem;. Which option correctly uses addUtilities in your Tailwind plugin?
AaddUtilities({ '.m-12': margin: '3rem' })
BaddUtilities({ '.m-12': { margin: 3rem } })
CaddUtilities({ '.m-12': { margin: '3rem' } })
DaddUtilities('.m-12 { margin: 3rem; }')
Attempts:
2 left
💡 Hint
Remember that CSS property values must be strings in JavaScript objects.
❓ rendering
intermediate
2:00remaining
Which custom utility class will apply a blue background color correctly?
You add this utility in your Tailwind plugin:
addUtilities({ '.bg-custom-blue': { 'background-color': '#1e40af' } })

Which HTML snippet will show a blue background when using this utility?
A<div class="bg-custom-blue">Hello</div>
B<div class="bg_custom_blue">Hello</div>
C<div class="bg-custom-blue" style="background-color: red;">Hello</div>
D<div class="bg-custom-blue"></div>
Attempts:
2 left
💡 Hint
Class names must match exactly and inline styles override classes.
❓ selector
advanced
2:00remaining
Which selector correctly targets hover state in addUtilities?
You want to create a utility .text-hover-red that changes text color to red on hover. Which option correctly defines this in addUtilities?
AaddUtilities({ '.text-hover-red': { '&:hover': { color: 'red' } } })
BaddUtilities({ '.text-hover-red': { ':hover': { color: 'red' } } })
CaddUtilities({ '.text-hover-red:hover': { ':color': 'red' } })
DaddUtilities({ '.text-hover-red:hover': { color: 'red' } })
Attempts:
2 left
💡 Hint
Pseudo-classes like :hover go directly in the selector string.
❓ layout
advanced
2:00remaining
How to create a custom utility for a flex container with centered items?
You want a utility class .flex-center that applies display: flex;, justify-content: center;, and align-items: center;. Which option correctly adds this utility?
AaddUtilities({ '.flex-center': { display: 'flex', 'justify-content': 'center', 'align-items': 'center' } })
BaddUtilities({ '.flex-center': { display: 'flex', justify-content: center, align-items: center } })
CaddUtilities({ '.flex-center': { display: flex, justify-content: center, align-items: center } })
DaddUtilities({ '.flex-center': { display: 'flex', justifyContent: 'center', alignItems: 'center' } })
Attempts:
2 left
💡 Hint
CSS properties with hyphens must be quoted in JavaScript objects.
❓ accessibility
expert
3:00remaining
Which custom utility correctly adds focus-visible outline for accessibility?
You want a utility .focus-outline that adds a visible outline only when the element is focused via keyboard (using :focus-visible). Which option correctly defines this in addUtilities?
AaddUtilities({ '.focus-outline:focus': { outline: '2px solid #2563eb', outlineOffset: '2px' } })
BaddUtilities({ '.focus-outline:focus-visible': { outline: '2px solid #2563eb', outlineOffset: '2px' } })
CaddUtilities({ '.focus-outline': { ':focus-visible': { outline: '2px solid #2563eb', outlineOffset: '2px' } } })
DaddUtilities({ '.focus-outline': { outline: '2px solid #2563eb', outlineOffset: '2px' } })
Attempts:
2 left
💡 Hint
Use the :focus-visible pseudo-class in the selector string to target keyboard focus.

Practice

(1/5)
1. What is the main purpose of using addUtilities in Tailwind CSS?
easy
A. To create custom CSS classes that can be reused in your HTML
B. To remove unused Tailwind CSS classes from the build
C. To automatically generate responsive breakpoints
D. To add JavaScript functionality to Tailwind components

Solution

  1. Step 1: Understand the role of addUtilities

    addUtilities is used to define new CSS classes with custom styles that Tailwind does not provide by default.
  2. Step 2: Compare with other options

    Options A, B, and D describe other Tailwind or unrelated features, not the purpose of addUtilities.
  3. Final Answer:

    To create custom CSS classes that can be reused in your HTML -> Option A
  4. Quick Check:

    addUtilities = create reusable custom classes [OK]
Hint: Remember: addUtilities adds new CSS classes [OK]
Common Mistakes:
  • Confusing addUtilities with purge or remove features
  • Thinking addUtilities adds JavaScript code
  • Assuming it auto-generates responsive breakpoints
2. Which of the following is the correct syntax to add a custom utility class .text-shadow with text-shadow: 2px 2px #000; using addUtilities?
easy
A. addUtilities('.text-shadow { text-shadow: 2px 2px #000; }')
B. addUtilities({ '.text-shadow': { 'text-shadow': '2px 2px #000' } })
C. addUtilities({ textShadow: '2px 2px #000' })
D. addUtilities({ '.text-shadow': 'text-shadow: 2px 2px #000;' })

Solution

  1. Step 1: Check the object format for addUtilities

    addUtilities expects an object where keys are class selectors and values are objects of CSS properties.
  2. Step 2: Validate each option

    addUtilities({ '.text-shadow': { 'text-shadow': '2px 2px #000' } }) correctly uses an object with selector key and CSS property object value. Options B and D use incorrect formats (string or wrong value type). addUtilities({ textShadow: '2px 2px #000' }) misses the selector key.
  3. Final Answer:

    addUtilities({ '.text-shadow': { 'text-shadow': '2px 2px #000' } }) -> Option B
  4. Quick Check:

    addUtilities needs selector: CSS object [OK]
Hint: Use object with selector keys and CSS property objects [OK]
Common Mistakes:
  • Passing CSS as a string instead of an object
  • Omitting the selector key for the utility
  • Using camelCase for CSS properties instead of kebab-case
3. Given this Tailwind plugin code snippet, what CSS class will be generated?
addUtilities({
  '.custom-border': {
    'border-width': '3px',
    'border-color': '#f00'
  }
})
medium
A. A class named .border-custom with 3px red border
B. A class named .custom-border with 1px black border
C. A class named .custom-border with 3px red border
D. No class will be generated due to syntax error

Solution

  1. Step 1: Identify the class name and CSS properties

    The key '.custom-border' defines the class name. The CSS properties set border width to 3px and border color to red (#f00).
  2. Step 2: Confirm no syntax errors

    The object syntax is correct, so the class will be generated as described.
  3. Final Answer:

    A class named .custom-border with 3px red border -> Option C
  4. Quick Check:

    Selector and CSS properties match output class [OK]
Hint: Class name is the object key, CSS inside is the style [OK]
Common Mistakes:
  • Confusing class name with CSS property names
  • Assuming default border width or color
  • Thinking syntax error prevents generation
4. Identify the error in this Tailwind plugin code that tries to add a .bg-gradient utility:
addUtilities({
  '.bg-gradient': 'background: linear-gradient(to right, #000, #fff);'
})
medium
A. Missing semicolon after addUtilities call
B. Class selector should not start with a dot
C. The property name 'background' is invalid in Tailwind
D. CSS properties must be inside an object, not a string

Solution

  1. Step 1: Check the value type for the class key

    The value for '.bg-gradient' must be an object with CSS properties, not a string.
  2. Step 2: Validate other options

    Class selectors should start with a dot (correct here). 'background' is a valid CSS property. Semicolon after function call is optional in JavaScript.
  3. Final Answer:

    CSS properties must be inside an object, not a string -> Option D
  4. Quick Check:

    CSS properties need object format [OK]
Hint: Use object for CSS properties, not string [OK]
Common Mistakes:
  • Passing CSS as a string instead of an object
  • Removing the dot from class selector
  • Thinking semicolon is mandatory after addUtilities
5. You want to create a custom utility .hover-scale that scales an element to 1.1 times its size on hover using addUtilities. Which is the correct way to add this utility with hover variant support?
hard
A. addUtilities({ '.hover-scale': { transform: 'scale(1.1)' } }, { variants: ['hover'] })
B. addUtilities({ '.hover-scale:hover': { transform: 'scale(1.1)' } })
C. addUtilities({ '.hover-scale': { ':hover': { transform: 'scale(1.1)' } } })
D. addUtilities({ '.hover-scale': { transform: 'scale(1.1)' } }, { variants: ['responsive'] })

Solution

  1. Step 1: Understand how to add variants with addUtilities

    Variants like 'hover' are added as a second argument object with a variants array.
  2. Step 2: Check each option's approach

    addUtilities({ '.hover-scale': { transform: 'scale(1.1)' } }, { variants: ['hover'] }) correctly adds the utility and enables hover variant. addUtilities({ '.hover-scale:hover': { transform: 'scale(1.1)' } }) incorrectly tries to put :hover in the selector key. addUtilities({ '.hover-scale': { ':hover': { transform: 'scale(1.1)' } } }) nests :hover inside CSS object, which is invalid. addUtilities({ '.hover-scale': { transform: 'scale(1.1)' } }, { variants: ['responsive'] }) uses 'responsive' variant, not hover.
  3. Final Answer:

    addUtilities({ '.hover-scale': { transform: 'scale(1.1)' } }, { variants: ['hover'] }) -> Option A
  4. Quick Check:

    Variants go in second argument as array [OK]
Hint: Add variants as second argument with variants array [OK]
Common Mistakes:
  • Putting :hover inside selector string
  • Nesting :hover inside CSS object
  • Using wrong variant name like 'responsive' instead of 'hover'
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