Using the Tailwind-style Selector py-1 [&>p]:inline — What It Does and When to Use It
This utility-style class combo targets vertical padding plus a child selector that changes paragraph display. It’s commonly seen in utility-first CSS tools (Tailwind CSS or similar) that support arbitrary variants and the bracketed child selector syntax.
What each part means
- py-1 — adds small vertical padding (top and bottom). In Tailwind CSS default scale, this equals 0.25rem (4px).
- [&>p]:inline — applies the
display: inline;style to direct childelements of the element carrying the utility. The&represents the parent selector;>ptargets immediate paragraph children.
Resulting behavior
When you apply py-1 [&>p]:inline to an element:
- The element receives small vertical padding.
- Any direct child
elements become inline-level, so they flow inline with surrounding content rather than each starting on its own line.
When to use
- To collapse paragraph blocks into inline flow while keeping spacing on the container.
- When transforming blocky CMS-generated paragraphs into inline text segments inside a small padded container (e.g., headings with paragraph fragments).
- For tight UI components where paragraphs should behave like spans without altering source HTML.
Example
HTML:
html
<div class=“py-1 [&>p]:inline”><p>First sentence.</p> <p>Second sentence.</p></div>
Rendered effect: Both sentences appear inline on one line (wrapping as needed), while the container keeps 0.25rem vertical padding.
Notes and caveats
- Only direct children
are affected; nested paragraphs deeper than one level aren’t targeted. - Changing paragraphs to inline removes block semantics (margins, full-width stacking). Ensure this won’t break accessibility or layout expectations.
- This syntax requires a CSS toolchain that supports arbitrary variants/child selectors (e.g., Tailwind CSS v3+ with JIT). If unsupported, the bracketed variant will be ignored.
Quick alternatives
- Use utility classes on the paragraphs themselves:
…
- .container > p { display:inline; } and add padding via
.container { padding: .25rem 0; }
Leave a Reply