π Introduction
The display: inline-flex property turns an element into aninline-level flex container. It behaves like display: flex but remains inline β meaning it flows with text and inline elements while still giving full flexbox layout power to its children. Perfect for inline components like badges, buttons with icons, pill tabs, and nav items. β¨
π― What Does display: inline-flex Do?
- Makes the element act like an inline element
- Children behave as flex items
- Allows flexbox alignment (justify, align, gap, wrapping, etc.)
- Does NOT break the surrounding text flow
β¨ Syntax
Syntax
selector {
display: inline-flex;
}π§© inline-flex vs flex
| Property | Behavior |
|---|---|
| display: flex | Block-level flex container (takes full width) |
| display: inline-flex | Inline-level flex container (fits its content width) |
π₯ Practical Examples
1. Inline Badge Group
Badge Group
.badge-group {
display: inline-flex;
gap: 10px;
}
.badge {
background: #6366f1;
color: #fff;
padding: 6px 12px;
border-radius: 20px;
}2. Icon + Text Button
Icon Button
.btn {
display: inline-flex;
align-items: center;
gap: 8px;
background: #0ea5e9;
padding: 10px 16px;
color: white;
border-radius: 6px;
}
.btn img {
width: 18px;
height: 18px;
}3. Inline Navigation Menu
Inline Nav
.nav {
display: inline-flex;
gap: 20px;
}
.nav a {
text-decoration: none;
color: #333;
}4. Input + Button Inline
Input Group
.input-group {
display: inline-flex;
gap: 5px;
}
.input-group input {
padding: 8px;
}
.input-group button {
padding: 8px 12px;
}π§ When to Use inline-flex?
- For in-text UI elements (labels, tags, chips)
- For buttons with icons
- For compact components inside paragraphs or headings
- For nav items arranged horizontally
- For mini control groups (input + button)
β οΈ Common Mistakes
- Using inline-flex when you need a full-width layout β use flexinstead
- Forgetting that inline-flex respects surrounding text baselines (use
vertical-alignif needed) - Expecting inline-flex elements to break to new lines automatically (wrap using
flex-wrapif needed)
πΌοΈ Visual Demo
Further details can be found at: https://dummyimage.com/900x220/eeeeee/000000&text=display%3A+inline-flex+demo
π Conclusion
display: inline-flex is ideal for building highly aligned, responsive, small UI components that sit naturally inside text flow. It gives you the full power of flexbox while keeping the element inline. Master it, and your UI components will look cleaner, more balanced, and far more professional.