Understanding the Tailwind Utility: py-1 [&>p]:inline
This article explains the Tailwind CSS utility combination py-1 [&>p]:inline, what it does, when to use it, and alternatives.
What it does
- py-1: adds padding on the y-axis (top and bottom) equal to Tailwind’s spacing unit
1(typically 0.25rem). - [&>p]:inline: uses Tailwind’s arbitrary selector feature to target direct child
elements and apply thedisplay: inline;style to them.
Combined, py-1 [&>p]:inline gives an element 0.25rem vertical padding and forces any immediate paragraph children to render inline.
When to use it
- You want compact vertical spacing around a container while keeping its direct paragraphs inline with surrounding text (for example, turning block paragraphs into inline flows inside a header or badge).
- You need to alter only direct
children without affecting deeper nested paragraphs or other elements.
Example HTML
html
<div class=“py-1 [&>p]:inline”><p>First inline paragraph.</p> <p>Second inline paragraph.</p> <span>Other element unaffected.</span></div>
Rendered result: both
elements behave like inline elements (flowing on the same line if space allows), while the container keeps 0.25rem padding top and bottom.
Notes & caveats
- Using
display: inlineremoves block behaviors ofsuch as automatic vertical margin and full-width stacking; adjust margins/padding accordingly. - The selector
[&>p]only targets direct children. Use[&p]to target all descendantelements if needed. - Arbitrary selectors require a recent Tailwind version that supports the syntax and may need escaping in some tooling contexts.
- For accessibility and semantics, consider whether changing paragraph display is appropriate; sometimes using
or adjusting markup is cleaner.
Alternatives
- &]:pl-6” data-streamdown=“unordered-list”>
- Make paragraphs inline in markup: use
instead ofwhen inline semantics are intended. - Use utility classes on the
elements directly:.…
- Target all descendants:
py-1 [&p]:inline.
Quick reference
- &]:pl-6” data-streamdown=“unordered-list”>
- py-1 → padding-top & padding-bottom = 0.25rem
- [&>p]:inline → direct-child
elements display: inline
Use this pattern when you need tight vertical spacing around a container and want direct paragraph children to behave inline without changing the HTML structure.
Leave a Reply