Arbitrary color values let you use any color you want in Tailwind CSS, not just the preset ones. This helps you match exact colors for your design.
Arbitrary color values in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
class="bg-[#123abc] text-[#f0e68c] border-solid border-[2px] border-[#ff6347]"<div class="bg-[#3490dc] p-4">Hello with blue background</div><p class="text-[rgb(255,99,71)]">Tomato colored text</p><button class="bg-[hsl(120,100%,50%)] text-white p-2 rounded">Green button</button><div class="border-solid border-[3px] border-[rgba(255,0,0,0.5)] p-3">Semi-transparent red border</div>This example shows a heading with a custom green text color, a paragraph with a custom dark red text color inside a light background box, and a button with a custom blue background that changes shade on hover.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Arbitrary Color Values Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex flex-col items-center gap-6 p-6"> <h1 class="text-[rgb(34,139,34)] text-3xl font-bold">Custom Green Text</h1> <div class="bg-[#ffdead] p-6 rounded shadow-md"> <p class="text-[#8b0000]">This paragraph has a custom dark red text color.</p> </div> <button class="bg-[hsl(210,100%,50%)] text-white px-4 py-2 rounded hover:bg-[hsl(210,100%,40%)] transition"> Custom Blue Button </button> </body> </html>
Always wrap your custom color value in square brackets to tell Tailwind it's an arbitrary value.
You can combine arbitrary colors with other Tailwind utilities like padding, margin, and rounded corners.
Use browser DevTools to inspect and tweak colors live for best results.
Arbitrary color values let you use any color by writing it inside square brackets.
You can use hex, rgb, rgba, hsl, or any valid CSS color format.
This helps you match exact colors without changing Tailwind's default settings.
Practice
bg-[#ff5733]Solution
Step 1: Understand Tailwind's default colors
Tailwind provides a set of default colors but sometimes you need a specific shade not included.Step 2: Use arbitrary color values
By writing colors inside square brackets likebg-[#ff5733], you can use any valid CSS color.Final Answer:
To use any custom color not in Tailwind's default palette -> Option BQuick Check:
Arbitrary colors = custom colors [OK]
- Thinking arbitrary colors create animations
- Confusing color usage with font size changes
- Using arbitrary colors for shadows instead of colors
Solution
Step 1: Check the correct use of square brackets
Arbitrary values must be inside square brackets exactly, sobg-[...]is correct.Step 2: Validate the color function syntax
The color functionrgba(255,0,0,0.5)is valid CSS and correctly placed inside brackets.Final Answer:
bg-[rgba(255,0,0,0.5)] -> Option AQuick Check:
Correct brackets + valid rgba = bg-[rgba(255,0,0,0.5)] [OK]
- Omitting square brackets
- Using parentheses instead of brackets
- Using rgb instead of rgba for transparency
text-[hsl(120,100%,50%)]Solution
Step 1: Understand HSL color values
HSL stands for hue, saturation, and lightness. Hue 120° corresponds to green.Step 2: Analyze saturation and lightness
100% saturation and 50% lightness means the color is bright and fully saturated green.Final Answer:
Bright green -> Option AQuick Check:
Hue 120° = green [OK]
- Confusing hue degrees with other colors
- Ignoring saturation and lightness effects
- Assuming default Tailwind colors instead of arbitrary
bg-[#12345G]Solution
Step 1: Check hex color validity
Hex colors only allow digits 0-9 and letters A-F (case-insensitive). 'G' is invalid.Step 2: Confirm brackets and prefix
Square brackets and prefixbg-are correct here.Final Answer:
Invalid hex color code due to 'G' -> Option CQuick Check:
Hex digits must be 0-9 or A-F [OK]
- Ignoring invalid hex characters
- Thinking brackets are missing
- Confusing prefix names
Solution
Step 1: Use correct dark mode prefix
Tailwind usesdark:prefix to apply styles in dark mode.Step 2: Use correct arbitrary color syntax
Colors must be inside square brackets with valid CSS color values.Step 3: Validate color values
RGB values must be between 0 and 255; bg-[rgb(255,255,255)] dark:bg-[rgb(300,300,300)] has invalid 300 values.Final Answer:
bg-[rgb(255,255,255)] dark:bg-[rgb(30,30,30)] -> Option DQuick Check:
Correct dark: prefix + valid colors = bg-[rgb(255,255,255)] dark:bg-[rgb(30,30,30)] [OK]
- Using wrong dark mode prefix syntax
- Omitting square brackets for arbitrary colors
- Using invalid RGB values above 255
