π What is fit-content:?
fit-content: is a CSS property keyword (not the function version) that allows you to size elements based on their content, but with certain constraints. It behaves differently from fit-content(), though both relate to adaptive sizing.
Note
π― Syntax
fit-content Property Usage
width: fit-content;
height: fit-content;
max-width: fit-content;
max-height: fit-content;You can apply fit-content to width, height, max-width, or max-height.
π What Does fit-content: Do?
When you use fit-content: as a value, the element will:
- π Shrink-wrap to fit its content
- β But will not overflow outside parent constraints
- π¦ Behave similarly to max-content sizing, but with restrictions
π§ͺ Basic Example
Simple fit-content Example
.tag {
width: fit-content;
padding: 6px 14px;
background: #e5e5e5;
border-radius: 6px;
}The width becomes only as wide as the text. Perfect for badges, tags, and pill buttons.
π Visual Example

π₯ fit-content: vs auto
| Value | Behavior |
|---|---|
| auto | Expands to fill available space unless restricted |
| fit-content | Shrinks to the smallest width that fits its content |
Note
π§ Where is fit-content: most useful?
- Pill-shaped buttons or chips
- Badges and labels
- Inline-flex elements that need exact width
- Cards or blocks with dynamic content width
- Sidebar or element width that depends strictly on content size
π§ͺ Example: Buttons That Fit Text
Button with fit-content
button {
width: fit-content;
padding: 8px 18px;
border-radius: 25px;
background: #007bff;
color: white;
}The button width matches text size β very common in modern UI chips.
π§± Example: Centering fit-content Blocks
Centering fit-content Elements
.wrapper {
display: flex;
justify-content: center;
}
.box {
width: fit-content;
background: #ffe082;
padding: 12px;
}Fit-content boxes center nicely because their width matches content width.
π¦ fit-content for Height
fit-content Height
.popup {
height: fit-content;
padding: 20px;
}Height only expands as needed β useful for modals or tooltips.
π fit-content vs fit-content()
| Feature | fit-content: | fit-content() |
|---|---|---|
| Type | Keyword | Function |
| Used In | width, height, max-width, max-height | Grid track sizing or element size with max-limit |
| Behavior | Shrink-wrap content | Shrink-wrap but capped at a max limit |
| Flexibility | Automatic based on content | More control (min/max logic) |
Note
β οΈ Common Mistakes
- Thinking fit-content: works like max-width (it doesnβt)
- Using fit-content in block-level elements expecting fluid width
- Not realizing parent width can force shrinking
- Confusing the keyword with the function version
π₯ Summary
fit-content: is a powerful CSS value that allows elements to shrink exactly to the width or height of their content. Itβs perfect for tags, chips, buttons, tooltips, or elements that must be sized dynamically based on what they contain.
Learn more βMDN Docs π