Creating Custom Utilities with addUtilities in Tailwind CSS
📖 Scenario: You are building a simple webpage and want to add a custom utility class to Tailwind CSS to create a special button style that is not available by default.
🎯 Goal: Create a custom utility class called .btn-custom using Tailwind's addUtilities function. This class should add a background color of #4CAF50, white text color, some padding, and rounded corners. Then use this class in your HTML button.
📋 What You'll Learn
Create a Tailwind CSS plugin that adds a custom utility class .btn-custom
The .btn-custom class must have background-color: #4CAF50;
The .btn-custom class must have color: white;
The .btn-custom class must have padding: 0.5rem 1rem;
The .btn-custom class must have border-radius: 0.375rem;
Use the .btn-custom class in an HTML button element
💡 Why This Matters
🌍 Real World
Custom utilities let you extend Tailwind CSS to fit your unique design needs without writing extra CSS files.
💼 Career
Knowing how to create custom utilities with addUtilities is useful for frontend developers to maintain consistent design systems and speed up styling.
Progress0 / 4 steps
1
Set up the Tailwind CSS plugin file
Create a file called tailwind.config.js and inside it, import plugin from tailwindcss/plugin. Then create an empty plugin using plugin(function() {}) and export the config object with an empty plugins array.
Tailwind
Hint
Remember to import plugin from tailwindcss/plugin and export a config object with a plugins array.
2
Add the custom utility with addUtilities
Inside the plugins array, add a plugin created with plugin(function({ addUtilities }) { ... }). Use addUtilities to add a new utility class .btn-custom with these CSS properties: background-color: '#4CAF50', color: 'white', padding: '0.5rem 1rem', and border-radius: '0.375rem'.
Tailwind
Hint
Use addUtilities inside the plugin function to add the .btn-custom class with the specified CSS properties.
3
Create the HTML file with a button using the custom class
Create an index.html file with a basic HTML5 structure. Inside the <body>, add a <button> element with the class btn-custom and the text Click Me. Also link the Tailwind CSS stylesheet properly.
Tailwind
Hint
Make sure to add the btn-custom class to the button and link the compiled Tailwind CSS file.
4
Build Tailwind CSS and verify the custom utility works
Run your Tailwind CSS build process to generate the CSS file (e.g., npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch). Then open index.html in a browser and verify the button has a green background, white text, padding, and rounded corners from the btn-custom class.
Tailwind
Hint
Make sure to run the Tailwind build command and open the HTML file in a browser to see the green button with white text and rounded corners.
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
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.
Step 2: Compare with other options
Options A, B, and D describe other Tailwind or unrelated features, not the purpose of addUtilities.
Final Answer:
To create custom CSS classes that can be reused in your HTML -> Option A
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
Step 1: Check the object format for addUtilities
addUtilities expects an object where keys are class selectors and values are objects of CSS properties.
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.
C. The property name 'background' is invalid in Tailwind
D. CSS properties must be inside an object, not a string
Solution
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.
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.
Final Answer:
CSS properties must be inside an object, not a string -> Option D
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?