py-1 [&>p]:inline
Overview
The utility of the CSS class selector pattern py-1 [&>p]:inline is a concise example of using Tailwind CSS (or similar utility-first frameworks) combined with a nested selector to control spacing and display behavior for direct child paragraph elements. This article explains what each part does, when to use it, and practical examples.
What it means
- py-1 — a utility class that applies vertical padding (padding-top and padding-bottom) of 0.25rem (4px) when using Tailwind’s default spacing scale.
- [&>p]:inline — a variant using Tailwind’s arbitrary selector feature where
&represents the current element;>ptargets direct childelements;:inlineapplies thedisplay: inlinerule to those children.
Combined, py-1 [&>p]:inline applies small vertical padding to the element itself and forces any direct child paragraph elements to display inline.
When to use
- Converting block paragraph children to inline without changing their semantics in HTML.
- Compacting text flow inside components (e.g., badges, tags, small info rows) where paragraphs are used for markup but inline flow is desired.
- Avoiding extra CSS files by using utility classes for targeted styling.
Examples
- Simple HTML
<div class=“py-1 [&>p]:inline”><p>First part,</p> <p>second part.</p></div>
Result: Both paragraphs render inline, separated only by normal inline spacing; the container has top and bottom padding of 0.25rem.
- With additional utilities
<div class=“py-1 [&>p]:inline text-sm space-x-1”> <p>Label:</p> <p class=“font-semibold”>Value</p></div>
Adds smaller text size, horizontal spacing between inline children, and bolding of the value.
Accessibility & semantics
Using display: inline on
elements changes visual flow but not their semantic role. Prefer using inline elements (like ) for truly inline content; convert paragraphs only when markup is fixed or generated and cannot be changed.
Caveats
- Tailwind’s arbitrary selector feature requires PostCSS/JIT mode and may need configuration in older versions.
- Targeting
> paffects only direct children; nested paragraphs remain unaffected. - Inline paragraphs will ignore vertical margin; use spacing utilities on the container instead.
Alternatives
- Use semantic inline elements (
) and apply utilities directly. - Create a component class in CSS:
.inline-paragraphs > p { display: inline; }when Tailwind config doesn’t allow arbitrary selectors.
Conclusion
py-1 [&>p]:inline is a compact pattern to add vertical padding to a container while forcing direct child paragraphs to behave like inline elements. It’s useful when working within utility-first CSS systems and dealing with fixed HTML structures.
Leave a Reply