py-1 [&>p]:inline — What it means and how to use it
This title looks like a CSS utility class pattern (Tailwind-like) combining spacing and a child selector: py-1 (vertical padding) paired with [&>p]:inline (apply display: inline to direct child
elements). Below is a concise explanation, examples, and when to use it.
Breakdown
- py-1: shorthand for padding-top and padding-bottom set to a small value (in Tailwind CSS, typically 0.25rem).
- [&>p]:inline: a JIT-style arbitrary variant that targets direct child
elements and sets their display toinline.
Purpose
Combine container vertical padding with forcing immediate paragraph children to render inline, useful when you want the container to maintain vertical spacing but keep its paragraphs inline (for flow or styling consistency).
Example (Tailwind JIT / arbitrary selector)
html
<div class=“py-1 [&>p]:inline bg-gray-100”><p>First paragraph — now inline.</p> <p>Second paragraph — also inline.</p> <span>Normal span.</span></div>
Rendered effect:
- The div gets small vertical padding.
- Each direct
becomes inline, so they flow horizontally like text rather than stacking.
When to use
- You want semantic
elements but need them to behave inline inside a padded container. - Styling inline paragraph chips, tags, or text segments while preserving container spacing.
- Avoiding extra markup (like wrapping text in spans) while keeping HTML semantic.
Caveats
- The selector
[&>p]only targets direct children; nestedinside other elements won’t be affected. - Inline paragraphs won’t accept vertical margin/padding as block elements do; inline layout differences may affect alignment.
- Ensure your Tailwind config allows arbitrary variants (JIT mode) and that your build supports such selector syntax.
Alternatives
- Use spans instead of
if inline is desired by default. - Apply
display: inline-blockto paragraphs if you need block-like sizing with inline flow. -
elements directly.
Leave a Reply