py-1 [&>p]:inline
This article explains the Tailwind CSS utility-style expression py-1 [&>p]:inline, what it does, why you might use it, and examples showing practical usage and caveats.
What it means
- py-1 — applies vertical padding (padding-top and padding-bottom) with Tailwind’s
1spacing value (usually 0.25rem by default). - [&>p]:inline — uses Tailwind’s arbitrary variant syntax to target direct child
elements and set theirdisplaytoinline.
Combined, py-1 [&>p]:inline applies small vertical padding to the element while forcing any direct paragraph children to render inline rather than block.
Why use this
- To keep semantic markup (using
for paragraphs) but change layout behavior for specific children without adding extra utility classes to each child. - Useful when you want inline flow for paragraphs—for example, when adding small helper text or inline labels inside a container that should remain part of the line flow.
- Keeps HTML cleaner: the parent controls child display behavior centrally.
Practical examples
- Inline paragraph snippets inside a container
<div class=“py-1 [&>p]:inline”><p>Username:</p> <input type=“text” class=“ml-2” /></div>
Here the
stays semantic but behaves like inline text next to the input.
- Multiple inline paragraphs with spacing
<div class=“py-1 [&>p]:inline [&>p]:mr-2”> <p>First</p> <p>Second</p> <p>Third</p></div>
Each paragraph becomes inline and gains right margin for separation.
- Combining with responsive variants
<div class=“py-1 sm:py-2 [&>p]:inline lg:[&>p]:block”> <p>Short summary</p> <p>More details</p></div>
Paragraphs are inline by default, but become block-level on large screens.
Caveats and tips
- The selector
[&>p]targets only direct children. Nested paragraphs (e.g., inside another wrapper) won’t be affected. - Arbitrary variants require a Tailwind configuration that allows them (Tailwind v3+ supports this by default).
- Use sparingly: changing semantics purely for presentation can confuse maintainers—document intent or prefer adding a small utility class to the child where appropriate.
- If you need to target all descendant
elements, use[&_p]:inline(underscore for any descendant) instead of&>p.
Summary
py-1 [&>p]:inline is a concise Tailwind pattern to add vertical padding while converting direct paragraph children to inline display. It’s handy for maintaining semantic markup with specific layout needs, especially in compact UI components.
Leave a Reply