The focus variant helps you change how an element looks when you click or tab to it. This makes it easier for people to see where they are on the page.
Focus variant in Tailwind
focus:<utility-class>
<button class="focus:outline-none focus:ring-2 focus:ring-blue-500">Click me</button><input class="focus:border-green-500 border-2 border-gray-300 p-2" type="text" placeholder="Type here">
<a href="#" class="focus:text-red-600">Link</a>
This page shows three elements: a button, an input box, and a link. Each changes style when you focus on it by clicking or tabbing. The button gets a blue ring, the input border turns green with a ring, and the link text turns red.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Focus Variant Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex flex-col items-center justify-center min-h-screen bg-gray-50 gap-6 p-4"> <button class="px-4 py-2 bg-blue-600 text-white rounded focus:outline-none focus:ring-4 focus:ring-blue-300"> Focus Me </button> <input type="text" placeholder="Focus me" class="border-2 border-gray-400 rounded px-3 py-2 focus:border-green-500 focus:ring-2 focus:ring-green-300 focus:outline-none" /> <a href="#" class="text-gray-700 underline focus:text-red-600 focus:no-underline"> Focusable Link </a> </body> </html>
Always keep focus styles visible for keyboard users to improve accessibility.
Use focus:outline-none carefully; always add another visible focus style if you remove the default outline.
Test focus styles by using the keyboard (Tab key) to navigate your page.
The focus variant in Tailwind applies styles only when an element is focused.
It helps users see which element is active, improving navigation and accessibility.
Use focus: before any utility class to add these styles.
Practice
focus: variant in Tailwind CSS do?Solution
Step 1: Understand the focus variant purpose
Thefocus:variant applies styles when an element receives keyboard or mouse focus, helping users identify the active element.Step 2: Compare with other states
Hover applies on mouse hover, click is not a Tailwind variant, and disabled is a different state. Only focus matches the description.Final Answer:
Applies styles only when an element is focused -> Option DQuick Check:
focus: = styles on focus [OK]
- Confusing focus with hover
- Thinking focus applies on click
- Mixing focus with disabled state
Solution
Step 1: Recall Tailwind variant syntax
Tailwind uses the formatvariant:utility, so focus variant isfocus:before the utility class.Step 2: Identify correct option
Onlyfocus:text-blue-500matches the correct syntax. Others misuse colons or underscores.Final Answer:
focus:text-blue-500 -> Option AQuick Check:
Variant prefix + colon + utility = correct [OK]
focus: prefix before utility class [OK]- Placing :focus after utility
- Using underscores instead of colon
- Appending -focus to utility class
<button class="text-gray-700 focus:text-red-600">Click me</button>
Solution
Step 1: Understand base and focus styles
The button has base text colortext-gray-700and changes totext-red-600only on focus.Step 2: Determine color on focus and blur
When focused,focus:text-red-600overrides base gray color. When not focused, base gray applies.Final Answer:
Red when focused, gray when not focused -> Option AQuick Check:
focus: overrides base style on focus [OK]
- Thinking focus styles apply always
- Confusing base and focus colors
- Assuming no color change on focus
<input class="border focus-border-blue-500" />
Solution
Step 1: Check variant syntax
The focus variant requires a colon after it, likefocus:border-blue-500. The code usesfocus-border-blue-500which is invalid.Step 2: Verify utility and element support
border-blue-500is a valid utility and inputs support focus styles, so no issues there.Final Answer:
Missing colon after focus -> Option CQuick Check:
Variant must have colon separator [OK]
- Omitting colon after focus
- Using dash instead of colon
- Assuming inputs can't have focus styles
Solution
Step 1: Set base styles
Usebg-green-500for normal background andborderto set border width (default border color).Step 2: Apply focus variants correctly
Usefocus:bg-yellow-400to change background on focus andfocus:border-blackto change border color to black only on focus.Step 3: Verify option correctness
bg-green-500 focus:bg-yellow-400 border focus:border-blackcorrectly uses base green background and border, and changes background and border color on focus. Others misuse border color or width.Final Answer:
bg-green-500 focus:bg-yellow-400 border focus:border-black -> Option BQuick Check:
Base border + focus:border-black = black border only on focus [OK]
- Adding base 'border-black' (always black border)
- Missing base 'border' width
- Forgetting 'focus:border-black' color
Ready to prove it?
Pass the test and this pattern moves unknown
