Performance: Focus-within variant
This affects the browser's style recalculation and paint when an element or its children receive focus.
<div class="focus-within:border-blue-500">
<input />
</div><div class="group"> <input class="group-focus-within:border-blue-500" /> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using focus-within on container | Minimal, only parent and focused child | 1 reflow on focus change | Low paint cost | [OK] Good |
| Using focus-within on many nested elements | High, many style recalculations | Multiple reflows on focus change | Higher paint cost | [X] Bad |
What does the focus-within variant in Tailwind CSS do?
focus-within variant targets the parent element when any child inside it receives focus.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>
focus-within variant applies styles to the parent when any child is focused.focus-within:bg-blue-200, which is the correct Tailwind syntax for this effect.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>
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>
focus:border-green-500, which applies styles only when the div itself is focused.focus-within: must be used instead of focus:.focus: on the parent instead of focus-within:. -> Option BYou 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>
focus-within: variant applies styles to the parent when any child element is focused.focus-within:border-blue-600 on the section, which highlights the border when input or textarea inside is focused.Pass the test and this pattern moves unknown