How can I make a div not larger than its contents?
I have a layout similar to:
<div>
<table>
</table>
</div>
I would like for the div to only expand to as wide as my table becomes.
To make a div only as wide as its contents, make the div a shrink‑to‑fit container: use display: inline-block, display: table, or width: fit-content (use inline‑block as a broad fallback). If the child is a table, remove any width: 100% on the table and tweak table-layout / white-space so the table’s intrinsic width is what the wrapper will track.
Contents
- Make a div fit its content — why divs expand
- Quick fixes to make a div fit content (inline-block / fit-content / table)
- If the child is a table — sizing & table-specific tips
- Browser support & fallbacks
- Examples & copy‑paste code
- Sources
- Conclusion
Make a div fit its content — why divs expand
A plain <div> is a block-level box and, by default, its computed width fills its containing block (the parent), so the wrapper will usually be wider than the table inside. Want the wrapper to wrap the table instead? You need to change the wrapper’s layout mode so the browser uses a shrink‑to‑fit algorithm rather than the default “fill available width” behavior.
Shrink‑to‑fit behavior is produced by things like display: inline-block, display: table, floats or intrinsic sizing (width: fit-content), so switching the wrapper to one of those modes makes the div only as wide as the table’s intrinsic width.
Quick fixes to make a div fit content (inline-block / fit-content / table)
Here are the simplest, battle‑tested options (each works without JavaScript).
- display: inline-block — simple and compatible
.wrapper { display: inline-block; vertical-align: top; /* optional */ }
- Pros: Widely supported, keeps element in normal flow (no clearing required), easy to center with
text-align:centeron parent. - Tip: If you see a tiny gap (inline whitespace) between inline-blocks, that’s normal; remove the space in HTML or collapse it with CSS.
- width: fit-content — explicit intrinsic sizing (modern browsers)
.wrapper {
/* fallback first, then modern property */
display: inline-block; /* fallback for older browsers */
width: fit-content; /* modern */
width: -webkit-fit-content; /* optional older WebKit */
width: -moz-fit-content; /* optional older Firefox */
}
- Pros: Expresses intent clearly; the browser uses the element’s intrinsic width.
- Cons: Not supported in some legacy browsers (see Browser support below). See MDN for details: https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content
- display: table — behaves like a table and shrinks to its contents
.wrapper { display: table; }
- Pros: No inline whitespace issues; reliable shrink behavior.
- Cons: Changes CSS box behavior (table formatting context), which might interact with children differently.
- float-based shrink (less recommended)
.wrapper { float: left; } /* requires clearing if you need normal flow */
- Pros: Shrinks to content.
- Cons: Removes element from normal flow — you likely need a clearing strategy.
For most cases use display:inline-block (fast, compatible) or width:fit-content (clean, modern) with a fallback.
If the child is a table — sizing & table-specific tips
Tables have their own sizing rules. If your table or CSS forces it to expand, the wrapper can’t shrink below the table’s width.
- Remove forced full widths: make sure you don’t have
table { width: 100%; }or awidth="100%"attribute. If the table is 100% wide, the wrapper will be full width too. - Let the table size itself:
table { width: auto; table-layout: auto; }so columns are sized by content. - Control wrapping in cells: use
td { white-space: nowrap; }to prevent wrapping (which increases width), or allow wrapping to let the table get narrower if that’s acceptable. - If you need a single column to take remaining space, combine
table-layout: fixedwith percentage widths on other cells (see related table sizing techniques on Stack Overflow: https://stackoverflow.com/questions/30228771/how-to-get-the-td-in-html-tables-to-fit-content-and-let-a-specific-td-fill).
Also check any CSS reset or framework styles — some frameworks set tables to width:100% by default.
Browser support & fallbacks
display: inline-block— wide support on modern and older browsers; safe fallback.display: table— well supported by modern browsers.width: fit-content— supported in current Chrome, Firefox, Edge and Safari; not supported in legacy IE. See MDN for specifics: https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content
Suggested progressive CSS pattern:
.wrapper {
display: inline-block; /* safe fallback */
width: -moz-fit-content; /* older Firefox */
width: -webkit-fit-content; /* older WebKit */
width: fit-content; /* modern */
}
If you must support very old IE, use display:inline-block and, if necessary, the old IE hack display:inline; zoom:1; on the wrapper (rare today).
Examples & copy‑paste code
- Minimal inline-block (centered)
<div style="text-align:center;">
<div class="wrapper-inline">
<table>
<tr><td>Short</td><td>Cells</td></tr>
</table>
</div>
</div>
<style>
.wrapper-inline { display: inline-block; vertical-align: top; border:1px solid #ddd; padding:6px; }
</style>
- fit-content with fallback
<div class="wrapper-fit">
<table>...</table>
</div>
<style>
.wrapper-fit {
display: inline-block; /* fallback */
width: -moz-fit-content;
width: -webkit-fit-content;
width: fit-content; /* modern */
border:1px solid #ddd; padding:6px;
}
</style>
- display:table (no inline gaps)
<div class="wrapper-table">
<table>...</table>
</div>
<style>
.wrapper-table { display: table; border:1px solid #ddd; padding:6px; }
</style>
- What to avoid
- If you have
table { width:100% }the wrapper won’t shrink. Remove that rule if you want shrink behavior.
Sources
- https://stackoverflow.com/questions/450903/how-can-i-make-a-div-not-larger-than-its-contents
- https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content
- https://www.hostm.com/how-to/css/set-div-width-to-content
- https://www.geeksforgeeks.org/css/how-to-set-div-width-to-fit-content-using-css/
- https://www.tutorialspoint.com/how-to-set-div-width-to-fit-content-using-css
- https://fixedbugs.io/css/how-to-div-fit-content
- https://www.tutorialrepublic.com/faq/how-to-make-a-div-not-larger-than-its-contents-using-css.php
- https://www.w3docs.com/snippets/css/how-to-make-a-div-not-larger-than-its-contents.html
- https://css-tricks.com/forums/topic/container-div-needs-to-fit-width-of-child-table-with-dynamic-td-elements/
- https://stackoverflow.com/questions/30228771/how-to-get-the-td-in-html-tables-to-fit-content-and-let-a-specific-td-fill
- https://fjolt.com/article/css-make-element-width-of-contents
- https://www.anycodings.com/1questions/2994241/how-can-i-make-a-div-not-larger-than-its-contents
Conclusion
To make a div fit its content, switch the wrapper to a shrink‑to‑fit mode — display:inline-block or display:table, or use width: fit-content with an inline‑block fallback. Double‑check that the table itself isn’t forced to full width (no width:100%) and tweak table-layout / white-space if cell contents are driving unexpected width.