π What is display: inline-grid?
display: inline-grid turns an element into a grid container just likedisplay: grid, but with one difference:it behaves like an inline element. This means the grid participates in inline flow β it sits inline with text or other inline elements.
π Why Use inline-grid?
- When you need a grid layout inside inline content
- When the grid should not take full block width
- When aligning grid containers horizontally
- When creating inline badges, buttons, or UI elements using grid
π How inline-grid Behaves
inline-grid:
- Does NOT start on a new line (unlike display: grid)
- Only takes up as much width as needed
- Can sit beside text and other inline elements
- Still offers full 2D grid layout capability
π§ͺ Example: Basic inline-grid Setup
Inline Grid Example
.tag {
display: inline-grid;
grid-template-columns: auto auto;
gap: 6px;
background: #e2e8f0;
padding: 6px 10px;
border-radius: 6px;
}
.tag span {
background: #cbd5e1;
padding: 4px 6px;
border-radius: 4px;
}This creates a compact inline badge using grid layout.
π Visual Behavior Comparison
| Property | Behaves Like | Layout Power |
|---|---|---|
| display: grid | Block element (full width) | Full 2D Grid |
| display: inline-grid | Inline element (flows with text) | Full 2D Grid |
Note
π§ Example: Using inline-grid for Tags or Pills
Tag Pills with inline-grid
.pill {
display: inline-grid;
grid-template-columns: auto 1fr;
gap: 8px;
padding: 6px 12px;
background: #f1f5f9;
border-radius: 20px;
}
.icon {
background: #94a3b8;
width: 20px;
height: 20px;
border-radius: 50%;
}
.text {
font-size: 14px;
}Perfect for notifications, badges, or UI labels.
π§© Example: Aligning Multiple inline-grid Elements
Inline Alignment
.wrapper .box {
display: inline-grid;
grid-template-columns: repeat(2, 30px);
gap: 4px;
margin-right: 10px;
background: #e2e8f0;
padding: 10px;
border-radius: 6px;
}All boxes appear inline, side-by-side, because they are inline-level grid containers.
π¦ inline-grid + Text Flow
One powerful use of inline-grid is aligning icons with text while maintaining a compact layout.
Icon + Text Alignment
.label {
display: inline-grid;
grid-template-columns: 20px auto;
align-items: center;
gap: 6px;
}β οΈ When NOT to Use inline-grid
- When you need full-width layouts (use grid instead)
- When stacking large sections vertically
- When layout alignment requires block-level behavior
Note
π₯ Summary
display: inline-grid brings the full power of CSS Grid into inline flow. It behaves like an inline element but gives you complete 2D layout control β perfect for badges, labels, icon-text components, inline UI elements, and compact structured content.
Learn more atMDN Docs π