py-1 [&>p]:inline
This article explains the utility-class-looking selector “py-1 [&>p]:inline” (common in utility-first CSS frameworks like Tailwind) and how to use it to control vertical padding and child paragraph display.
What it means
- py-1 — applies small vertical padding (padding-top and padding-bottom). In Tailwind CSS,
py-1typically equals 0.25rem (4px) by default. - [&>p]:inline — uses a variant with a CSS selector target to style direct child paragraph elements (
> p), setting theirdisplayproperty toinline. The bracket syntax lets you pass arbitrary selectors or rules to the utility framework so they become part of the generated CSS.
Combined, this class sequence:
- Adds vertical spacing around the element.
- Forces any direct
child to render inline instead of the browser default block.
Why use it
- Prevents paragraphs from creating vertical breaks inside a container while keeping overall vertical spacing on the container itself.
- Useful for compact inline groupings of text where semantic paragraphs are preferred but the layout requires inline flow (e.g., inline disclaimers, taglines, or mixed inline elements).
Example HTML
<div class=“py-1 [&>p]:inline”><p>First sentence.</p> <p>Second sentence follows inline.</p> <span> — and a span continues the line.</span></div>
Rendered effect:
- The container gets small top/bottom padding.
- The two
elements sit on the same line as inline content, separated only by normal inline spacing, not block breaks.
Considerations
- Inline paragraphs still retain margin, font, and other text properties; you may need to reset default paragraph margins (
m-0ormt-0 mb-0) if unwanted gaps remain. - Accessibility/semantics: using
for paragraphs but displaying them inline is fine when content remains semantically paragraph text; ensure it doesn’t confuse assistive tech or expected reading order.
- Browser support: this is a styling approach; it works wherever the underlying CSS is supported. The framework compiles the selector into standard CSS targeting
> p.
Quick tweaks
- Remove paragraph margins:
- class=“py-1 [&>p]:inline [&>p]:m-0”
- Make child paragraphs inline-block instead (so width/padding apply):
- class=“py-1 [&>p]:inline-block”
Summary
“py-1 [&>p]:inline” is a concise way in utility-first CSS to give a container small vertical padding while forcing its direct paragraph children to flow inline. Use it when you want semantic paragraphs to behave like inline text without extra block spacing.
Leave a Reply