Square bracket notation lets you use any value you want in Tailwind styles. It helps when the default options don't fit your design.
Square bracket notation for custom values in Tailwind
class="property-[value]"Replace property with the Tailwind utility name like bg, w, m, etc.
Put your custom value inside square brackets exactly as you want it in CSS, for example [10px] or [var(--my-color)].
<div class="bg-[#ff6347] p-[15px]">Hello</div><div class="w-[250px] h-[100px] bg-blue-200">Box</div><p class="text-[1.75rem] leading-[2rem]">Custom text size and line height</p>This page shows three elements using square bracket notation for custom colors, sizes, and spacing. You see a green box with 20px padding, an orange box sized 300 by 150 pixels, and a paragraph with custom text size and line height.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Tailwind Custom Values</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex flex-col items-center justify-center min-h-screen gap-6 bg-gray-50"> <div class="bg-[#4caf50] p-[20px] rounded text-white font-semibold"> Custom green background with 20px padding </div> <div class="w-[300px] h-[150px] bg-[#ff5722] flex items-center justify-center text-white font-bold"> 300px by 150px box </div> <p class="text-[2rem] leading-[2.5rem] text-gray-700"> Custom font size and line height </p> </body> </html>
Always include the exact CSS value inside the brackets, including units like px, rem, or color codes.
Square bracket notation works with most Tailwind utilities but not all. Check Tailwind docs if unsure.
Use this to quickly try custom styles without changing your Tailwind config file.
Square bracket notation lets you add any CSS value inside Tailwind classes.
Use it when Tailwind's default values don't match your design needs.
Remember to include units and proper CSS syntax inside the brackets.
Practice
Solution
Step 1: Understand Tailwind's default values
Tailwind provides a set of predefined values for spacing, colors, sizes, etc.Step 2: Role of square bracket notation
Square bracket notation lets you write any CSS value directly inside a class, likemt-[10px]for margin-top 10 pixels.Final Answer:
Use custom CSS values not included in Tailwind's default set -> Option DQuick Check:
Square brackets = custom CSS values [OK]
- Thinking it creates new permanent classes
- Confusing with JavaScript code inside classes
- Assuming it disables Tailwind styles
Solution
Step 1: Identify correct Tailwind prefix for width
Tailwind usesw-prefix for width utilities.Step 2: Use square brackets with full CSS value
Inside brackets, you must include the unit, so250pxis correct.Final Answer:
w-[250px] -> Option BQuick Check:
Width with unit inside brackets = w-[250px] [OK]
- Omitting units inside brackets
- Using wrong prefix like width-
- Not using brackets for custom values
class="w-[15rem]" in Tailwind CSS?Solution
Step 1: Understand the CSS unit inside brackets
The classw-[15rem]sets width to 15 rem units, where 1 rem equals the root font size (usually 16px).Step 2: Interpret the rendered size
15 rem means 15 times the root font size, so it is not pixels or percent but relative to font size.Final Answer:
15 rem units (usually 15 times root font size) -> Option AQuick Check:
Square bracket value with rem = relative font size width [OK]
- Assuming rem means pixels
- Confusing rem with percent or viewport units
- Ignoring units inside brackets
class="p-[20]" to add padding of 20 pixels, but it doesn't work. What is the error?Solution
Step 1: Check the syntax of the custom value
Inside square brackets, you must include the CSS unit, like20px, not just a number.Step 2: Understand Tailwind's requirement for units
Without units, the CSS value is invalid, so the style won't apply.Final Answer:
Missing unit inside square brackets -> Option CQuick Check:
Always include units inside brackets for custom values [OK]
- Omitting units inside brackets
- Using wrong class names like padding-
- Thinking padding can't be customized
Solution
Step 1: Use correct Tailwind prefixes
Background color usesbg-and border radius usesrounded-.Step 2: Apply square bracket notation with full CSS values
Inside brackets, include the full hex color#1a73e8and the unit12pxfor radius.Step 3: Check for syntax correctness
bg-[#1a73e8] rounded-[12px] uses correct prefixes and proper bracket notation with units and hash symbol.Final Answer:
bg-[#1a73e8] rounded-[12px] -> Option AQuick Check:
Use correct prefixes + full CSS values in brackets [OK]
- Omitting # in hex color
- Missing units for border radius
- Using wrong prefixes like background-
Ready to prove it?
Pass the test and this pattern moves unknown
