py-1 [&>p]:inline
This article explains the utility and mechanics of the Tailwind CSS utility-like pattern shown in the title: “py-1 [&>p]:inline”. It’s a compact way to combine vertical padding with a selector-targeted rule for direct child paragraphs. Below you’ll find what it does, when to use it, how it works, and examples.
What it does
- py-1 applies vertical padding (padding-top and padding-bottom) of Tailwind’s spacing scale value 1.
- [&>p]:inline applies the CSS display: inline rule to immediate child
elements of the element that has this class.
Together, the combined class string is useful when you want a container with small vertical padding and you want any direct paragraph children to behave like inline elements (removing paragraph block spacing and allowing them to flow inline).
When to use it
Use this pattern when:
- You need a compact container with light vertical spacing.
- Paragraph tags are used for semantic markup but you want them to render inline (for example, combining multiple small phrases separated by
tags, or when adjusting legacy markup without changing HTML).
- You want to avoid creating additional CSS and prefer a Tailwind-first approach.
Avoid it when:
- You control the HTML and can replace
with or other inline elements—then semantic clarity may suggest directly using inline elements.
- You need to target nested or non-direct descendant
elements (this pattern only matches immediate children).
How it works (technical)
- py-1 expands to:
- padding-top: 0.25rem;
- padding-bottom: 0.25rem;
(assuming Tailwind’s default spacing scale where 1 = 0.25rem)
- [&>p]:inline uses Tailwind’s arbitrary variants feature. It generates a selector like:
.your-class[&>p]:inline => .your-class > p { display: inline; }
The [&>p] part is inserted as a custom variant that becomes the selector targeting direct child p elements. The :inline part is the utility applied under that selector, producing display: inline.
Generated CSS (approximate):
.py-1 { padding-top: 0.25rem; padding-bottom: 0.25rem; }.py-1 > p { display: inline; }
Examples
- Basic HTML
<div class=“py-1 [&>p]:inline”><p>First</p> <p>Second</p></div>
Renders as: FirstSecond (inline flow), with small vertical padding around the container.
- With spacing between inline paragraphs
<div class=“py-1 [&>p]:inline [&>p]:mr-2”> <p>Alpha</p> <p>Beta</p></div>
Adds right margin between the inline paragraphs.
- Semantic alternative (if you control markup)
If you can change the HTML, prefer:
<div class=“py-1”> <span>First</span> <span>Second</span></div>
This avoids the need for selector overrides and preserves intent.
Accessibility & semantics
- Converting paragraphs to inline visually is fine for presentation, but ensure the semantic meaning of paragraphs isn’t important for assistive tech or document structure. If the text fragments are not paragraphs conceptually, prefer inline elements in HTML.
Summary
“py-1 [&>p]:inline” is a concise Tailwind pattern to give an element light vertical padding while forcing direct child paragraphs to render inline. It’s handy for quick layout fixes and working with markup you can’t change, but prefer semantic HTML when possible.
Leave a Reply