0
0
Tailwindmarkup~20 mins

Arbitrary variants for custom selectors in Tailwind - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arbitrary Variant Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
selector
intermediate
2:00remaining
Using an arbitrary variant to style a child element
Given the following HTML, which Tailwind class will correctly apply a red text color to the only when its parent
is hovered?
Tailwind
<div class="group">
  <span>Hover me</span>
</div>
Ahover:[&>span]:text-red-500
B[&>span]:group-hover:text-red-500
Cgroup-hover:[&>span]:text-red-500
Dgroup-hover:text-red-500 [&>span]
Attempts:
2 left
💡 Hint
Think about how to combine group-hover with an arbitrary variant targeting the direct child span.
layout
intermediate
2:00remaining
Applying styles to sibling elements using arbitrary variants
Which Tailwind class applies a blue background to a

element only when the preceding

sibling is focused?

Tailwind
<h2 tabindex="0">Title</h2>
<p>Paragraph</p>
Afocus:[&+p]:bg-blue-500
B[&:focus+p]:bg-blue-500
C[&+p:focus]:bg-blue-500
D[&:focus~p]:bg-blue-500
Attempts:
2 left
💡 Hint
Use the adjacent sibling combinator (+) with focus on the

.

🧠 Conceptual
advanced
2:00remaining
Understanding arbitrary variant specificity
What is the effect of the Tailwind class [&:not(:last-child)]:mb-4 on a list of
  • elements?
  • AAdds a bottom margin of 1rem to all <li> elements including the last one.
    BAdds a bottom margin of 1rem only to the last <li> element.
    CRemoves bottom margin from all <li> elements.
    DAdds a bottom margin of 1rem to all <li> elements except the last one.
    Attempts:
    2 left
    💡 Hint
    The :not(:last-child) selector excludes the last child from styling.
    accessibility
    advanced
    2:00remaining
    Using arbitrary variants for focus-visible styling
    Which Tailwind class correctly applies a visible outline only when a button receives keyboard focus (focus-visible)?
    Tailwind
    <button>Click me</button>
    Afocus-visible:outline-2 focus-visible:outline-blue-500
    B[&:focus-visible]:outline-2 [&:focus-visible]:outline-blue-500
    C[&:focus]:outline-2 [&:focus]:outline-blue-500
    Dfocus:outline-2 focus:outline-blue-500
    Attempts:
    2 left
    💡 Hint
    Tailwind supports focus-visible as a variant without needing arbitrary selectors.
    📝 Syntax
    expert
    3:00remaining
    Identifying the correct syntax for nested arbitrary variants
    Which Tailwind class correctly applies a green text color to all
  • elements inside a
      only when the
        is hovered?
  • Tailwind
    <ul class="hover:[&>li]:text-green-500">
      <li>Item 1</li>
      <li>Item 2</li>
    </ul>
    A[&:hover>li]:text-green-500
    Bhover:[&>li]:text-green-500
    C[&>li:hover]:text-green-500
    Dhover:[&:hover>li]:text-green-500
    Attempts:
    2 left
    💡 Hint
    The arbitrary variant should wrap the hover state on the parent and target the children.