py-1 [&>p]:inline
This article explains the utility and mechanics of the Tailwind CSS-style utility class pattern shown in the title: py-1 [&>p]:inline. It covers what each part means, why you might use it, practical examples, and alternatives.
What it means
- py-1: a shorthand utility that applies vertical padding (padding-top and padding-bottom) of 0.25rem (assuming Tailwind’s default scale) to the element.
- [&>p]:inline: a Tailwind JIT-style arbitrary variant selector that targets direct child
elements of the current element and applies theinlinedisplay utility to them. In other words, inside this element, any immediatechildren will be rendered inline rather than using their default block display.
Why use this pattern
- Keeps parent spacing consistent while altering how direct child paragraphs are laid out without adding extra classes to each child.
- Useful when you want inline flow for paragraphs (e.g., for small badges, metadata spans, or mixed inline content) while still keeping uniform vertical padding around the container.
- Leverages Tailwind’s utility-first approach for concise, maintainable markup without custom CSS.
Practical examples
- Simple container with inline paragraphs
<div class=“py-1 [&>p]:inline”><p>Label:</p> <p>Value</p></div>
Result: Both paragraphs appear inline as “Label: Value” with 0.25rem vertical padding on the container.
- Inline paragraphs mixed with other inline elements
<div class=“py-1 [&>p]:inline gap-2 flex-wrap”> <p>First</p> <span class=“text-gray-500”>•</span> <p>Second</p> <a href=”#” class=“text-blue-600”>Link</a></div>
Note: In this example, adding layout utilities like flex or inline-flex to the parent may be necessary depending on desired wrapping and alignment; the [&>p]:inline only changes the display of direct p children.
- Using with responsive variants
<div class=“py-1 md:py-2 [&>p]:inline md:[&>p]:block”> <p>Short intro</p> <p>More detailed sentence that becomes block on medium screens</p></div>
Result: Paragraphs are inline on small screens and switch to block on medium and up.
Caveats and tips
- The arbitrary variant syntax (
[selector]:utility) relies on Tailwind’s Just-In-Time engine or support for arbitrary variants; ensure your build setup enables them. - The selector
&>ptargets only direct children. Use& pto target all descendant p elements or a more specific selector if needed. - Applying
inlinetoelements removes their vertical margins and block flow; adjust spacing with margin or padding utilities on either the parent or the children. - If you need more complex behavior (e.g., different styles for first/last child), combine arbitrary variants with pseudo-class selectors:
[&>p:first-child]:mr-2.
Alternatives
- Add a class to each
(e.g.,class=“inline”) — more verbose but straightforward without JIT features. - Use component-level CSS (e.g.,
.my-container > p { display: inline; }) if you prefer CSS files over utilities.
Conclusion
py-1 [&>p]:inline is a concise Tailwind pattern to give a container light vertical padding while forcing its direct paragraph children to display inline. It’s powerful for small layout adjustments, but requires familiarity with Tailwind’s arbitrary variants and careful attention to selector specificity and layout context.
Leave a Reply