The focus-within variant helps style a parent element when any child inside it is focused. This makes it easier to highlight groups of inputs or sections when users interact with them.
Focus-within variant in Tailwind
focus-within:<utility>
Use focus-within: before any Tailwind utility to apply styles when a child element is focused.
This variant works on the parent element, not the focused child itself.
<div class="focus-within:border-blue-500 border-2 p-4"> <input type="text" /> </div>
<section class="focus-within:bg-gray-100 p-6">
<textarea></textarea>
</section><nav class="focus-within:ring-2 focus-within:ring-indigo-500"> <a href="#">Link</a> </nav>
This form container gets a blue glowing ring around it when any input inside is focused. This helps users see which form section they are working on.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Focus-within Variant Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex items-center justify-center min-h-screen bg-gray-50"> <form class="focus-within:ring-4 focus-within:ring-blue-400 border-2 border-gray-300 rounded-lg p-6 w-80"> <label for="name" class="block mb-2 font-semibold">Name</label> <input id="name" type="text" class="w-full p-2 border border-gray-400 rounded focus:outline-none focus:ring-2 focus:ring-blue-600" placeholder="Enter your name" /> <label for="email" class="block mt-4 mb-2 font-semibold">Email</label> <input id="email" type="email" class="w-full p-2 border border-gray-400 rounded focus:outline-none focus:ring-2 focus:ring-blue-600" placeholder="Enter your email" /> </form> </body> </html>
The focus-within variant works well for grouping related inputs visually.
It improves keyboard navigation by showing focus on the container, not just the input.
Remember to use semantic HTML and labels for better accessibility.
focus-within styles a parent when any child inside is focused.
Use it to highlight form sections or groups of controls.
It helps users see where they are interacting on the page.
Practice
What does the focus-within variant in Tailwind CSS do?
Solution
Step 1: Understand the focus-within behavior
Thefocus-withinvariant targets the parent element when any child inside it receives focus.Step 2: Compare options with this behavior
Only It applies styles to a parent element when any child element inside it is focused. correctly describes this behavior, while others describe different or incorrect behaviors.Final Answer:
It applies styles to a parent element when any child element inside it is focused. -> Option AQuick Check:
focus-within targets parent on child focus [OK]
- Thinking focus-within styles only the focused child
- Confusing focus-within with focus-visible
- Assuming it styles siblings of the focused element
Which of the following is the correct Tailwind CSS syntax to change the background color of a <div> when any child inside it is focused?
<div class="?"> <input type="text" /> </div>
Solution
Step 1: Identify the correct variant for parent styling on child focus
Thefocus-withinvariant applies styles to the parent when any child is focused.Step 2: Match the variant with the correct syntax
focus-within:bg-blue-200 usesfocus-within:bg-blue-200, which is the correct Tailwind syntax for this effect.Final Answer:
focus-within:bg-blue-200 -> Option CQuick Check:
Parent focus style on child focus = focus-within:bg-color [OK]
- Using focus: which styles only the focused element
- Using non-existent focus-child variant
- Confusing focus-visible with focus-within
Given this HTML and Tailwind CSS, what will happen when the user clicks inside the input?
<div class="border-2 border-gray-300 focus-within:border-green-500 p-4"> <input type="text" class="outline-none" /> </div>
Solution
Step 1: Understand focus-within effect on the div
The div has a gray border by default and changes border color to green when any child (input) is focused.Step 2: Identify what changes on input focus
When the input is focused, the div's border changes to green, not the input's border or background color.Final Answer:
The div's border color changes to green when the input is focused. -> Option DQuick Check:
focus-within changes parent border color [OK]
- Thinking input border changes instead of div border
- Assuming background color changes without code
- Believing focus-within doesn't apply to div
What is wrong with this Tailwind CSS usage if the goal is to highlight the parent div's border when the input inside is focused?
<div class="border-2 border-gray-300 focus:border-green-500 p-4"> <input type="text" class="outline-none" /> </div>
Solution
Step 1: Identify the variant used on the parent div
The parent div usesfocus:border-green-500, which applies styles only when the div itself is focused.Step 2: Understand the correct variant for child focus affecting parent
To style the parent when a child is focused,focus-within:must be used instead offocus:.Final Answer:
Usingfocus:on the parent instead offocus-within:. -> Option BQuick Check:
Parent highlight on child focus needs focus-within: [OK]
- Using focus: instead of focus-within:
- Thinking input needs focus-visible class
- Believing border order affects focus styles
You want to create a form section that highlights its border in blue when any input or textarea inside is focused. Which Tailwind CSS class combination correctly achieves this?
<section class="?"> <input type="text" /> <textarea></textarea> </section>
Solution
Step 1: Identify the variant that styles parent on child focus
Thefocus-within:variant applies styles to the parent when any child element is focused.Step 2: Check the class combinations for correct usage
border-2 border-gray-400 focus-within:border-blue-600 p-6 rounded-lg usesfocus-within:border-blue-600on the section, which highlights the border when input or textarea inside is focused.Final Answer:
border-2 border-gray-400 focus-within:border-blue-600 p-6 rounded-lg -> Option AQuick Check:
Parent border highlight on child focus = focus-within:border-color [OK]
- Using focus: which styles only the container if focused
- Using focus-visible: which targets keyboard focus only
- Using non-existent hover-within: variant
Ready to prove it?
Pass the test and this pattern moves unknown
