Question
I have HTML like this:
<div>
<table>
</table>
</div>
I want the div to expand only as wide as the table inside it, instead of taking up the full available width. How can I make a div no wider than its contents?
Short Answer
By the end of this page, you will understand why a div normally stretches to fill its container, how CSS display behavior affects width, and how to make an element shrink to fit its contents. You will also see practical examples, common mistakes, and when to use alternatives like inline-block, inline, or fit-content.
Concept
In HTML and CSS, a plain div is a block-level element by default. That means it usually takes up the full available width of its parent container, even if the content inside it is much smaller.
So in this markup:
<div>
<table></table>
</div>
the table may only be 300px wide, but the div will still often stretch across the parent.
To make the div match the width of its content, you need to change how the browser lays it out.
A common solution is:
div {
display: inline-block;
}
This makes the div behave like an inline-level box that can still have block-like dimensions, and its width will usually shrink to fit its content.
Another modern option is:
div {
width: fit-content;
}
This tells the browser to size the element based on its contents. However, inline-block is often simpler and more widely familiar.
This matters because controlling element width is a basic part of layout design. Developers use it for cards, labels, wrappers, badges, menus, tooltips, and many UI components that should only be as wide as their content.
Mental Model
Think of a normal div like a shelf mounted across an entire wall. Even if you place one small object on it, the shelf still spans the full width.
Changing the div to inline-block or using fit-content is like replacing that wall-wide shelf with a small tray that grows only as large as the object sitting on it.
- Block element (
div): takes the whole row - Shrink-to-fit element: only takes the space it needs
So your goal is to make the wrapper act like a tray instead of a full-width shelf.
Syntax and Examples
Basic solution with inline-block
<div class="wrapper">
<table>
<tr>
<td>Name</td>
<td>Score</td>
</tr>
</table>
</div>
.wrapper {
display: inline-block;
}
Why this works
A normal div is display: block, which fills available width. Setting it to inline-block lets it size itself based on its content.
Modern option with fit-content
.wrapper {
width: fit-content;
}
This can also make the wrapper match the width of the table.
Full example
<div class="wrapper">
<table border="1">
<tr>
<td>Product</td>
<td>Price</td>
</tr>
<tr>
<td>Notebook</td>
<td>$5</td>
</tr>
</table>
</div>
.wrapper {
display: inline-block;
border: 2px solid steelblue;
padding: 8px;
}
The blue border will wrap closely around the table instead of stretching across the page.
Step by Step Execution
Consider this code:
<div class="wrapper">
<table border="1">
<tr>
<td>A</td>
<td>B</td>
</tr>
</table>
</div>
.wrapper {
display: inline-block;
padding: 10px;
border: 1px solid black;
}
Here is what happens:
- The browser reads the
divwith classwrapper. - By default, a
divwould bedisplay: blockand stretch across the parent. - The CSS changes it to
display: inline-block. - Now the browser calculates the width based on the content inside the
div. - The
tabledetermines most of that width. - The
paddingandborderare added around the table. - The final
divwidth becomes: table width + left/right padding + left/right border.
If the table becomes wider, the wrapper grows with it. If the table is narrow, the wrapper stays narrow too.
Real World Use Cases
This pattern is useful in many real layouts:
- Wrapping a table tightly so a border or background only surrounds the table itself
- Badges and tags that should be only as wide as their text
- Tooltip boxes that should size to their content
- Dropdown menus that should not stretch full width
- Cards or panels that should hug small amounts of content
- Status labels like
Success,Pending, orFailed
Example:
<div class="status">Pending Review</div>
.status {
display: inline-block;
padding: 4px 10px;
background: gold;
border-radius: 4px;
}
The element stays only as wide as the text plus padding.
Real Codebase Usage
In real projects, developers often use this concept in small reusable UI wrappers.
Common patterns
Content-sized wrappers
Used for pills, labels, chips, and compact panels.
.chip {
display: inline-block;
padding: 0.25rem 0.5rem;
}
Guarding layout width
Sometimes developers combine shrink-to-fit behavior with a maximum width.
.tooltip {
display: inline-block;
max-width: 300px;
}
Form and data display components
A table wrapper may be given a border, shadow, or background without taking full width.
.table-panel {
display: inline-block;
background: white;
border: 1px solid #ccc;
}
Utility classes
In larger codebases, teams often create utility classes instead of repeating custom CSS.
.w-fit {
width: fit-content;
}
.inline-block {
display: inline-block;
}
This keeps layouts consistent and easy to reuse.
Common Mistakes
1. Expecting a default div to shrink automatically
Broken expectation:
<div class="box">
<table></table>
</div>
.box {
border: 1px solid red;
}
A block div still stretches full width.
Fix
.box {
display: inline-block;
}
2. Using display: inline instead of inline-block
.box {
display: inline;
}
This may not behave as expected for width, height, padding, or block-style layout.
Better
.box {
display: inline-block;
}
3. Forgetting parent layout rules
If the parent uses flexbox or grid, sizing behavior may be affected by alignment and item sizing rules.
Example:
.parent {
display: flex;
}
A flex item does not always behave exactly like a normal block element.
4. Assuming width: auto means content width
For block elements, width: auto usually means filling available width, not shrinking to content.
5. Ignoring padding and borders
Even if the content is narrow, the final visible size includes:
- content width
- left and right padding
- left and right border
So the wrapper may be wider than the table by design.
Comparisons
| Approach | What it does | Good for | Notes |
|---|---|---|---|
display: block | Takes full available width | Standard page sections | Default for div |
display: inline | Flows with text | Small inline text elements | Width/height control is limited |
display: inline-block | Shrinks to content but still supports box styling | Wrappers, badges, table containers | Common practical choice |
width: fit-content | Sizes width to content | Modern content-sized containers | Clear intent, but layout context still matters |
display: flex on parent with custom alignment | Can prevent stretching in some layouts | Flexible UI components | Depends on container rules |
inline-block vs fit-content
| Feature | inline-block | fit-content |
|---|---|---|
| Easy to understand | Yes | Yes |
| Changes display type | Yes | No |
| Shrinks to content | Yes | Yes |
| Common beginner solution | Yes | Moderate |
| Useful for wrappers | Yes | Yes |
If you want a simple answer for this case, display: inline-block is usually the most straightforward choice.
Cheat Sheet
Quick fix
.wrapper {
display: inline-block;
}
Alternative
.wrapper {
width: fit-content;
}
Key rule
- A normal
divis a block element. - Block elements usually stretch to the full width of their parent.
- To make it match content width, use shrink-to-fit behavior.
Good choices
display: inline-blockfor compact wrapperswidth: fit-contentfor content-sized width
Watch out for
- Parent flex or grid rules
- Extra padding and borders
- Mistaking
width: autofor content width
Typical use cases
- Table wrappers
- Tags and badges
- Tooltips
- Compact boxes
- Labels
FAQ
Why does a div take the full width by default?
Because a div is a block-level element, and block elements normally expand to fill the available width of their parent.
What is the easiest way to make a div fit its contents?
Usually, set display: inline-block on the div.
Can I use width: fit-content instead?
Yes. It is a good modern option for content-based sizing.
Is display: inline the same as inline-block?
No. inline elements do not behave the same for sizing and box layout. inline-block is usually the better choice here.
Will this work with a table inside the div?
Yes. The wrapper can shrink to the width of the table.
Why is my wrapper still wider than the table?
Check for padding, borders, margins, or parent layout rules such as flexbox or grid.
Does width: auto make the div match its content?
Not for a normal block element. width: auto usually means fill available space.
Which solution should I prefer in beginner code?
display: inline-block is the most common and easy-to-understand solution.
Mini Project
Description
Build a small data panel that displays a table inside a bordered wrapper which should only be as wide as the table. This demonstrates how to prevent a wrapper div from stretching across the page and how content-based sizing is used in practical UI design.
Goal
Create a compact table container whose border and background hug the table instead of filling the entire row.
Requirements
- Create an HTML page with a wrapper
divcontaining atable. - Add at least two rows of sample data.
- Style the wrapper so it only grows as wide as the table content.
- Add padding, border, and background color to make the wrapper visible.
Keep learning
Related questions
Add a Favicon to a Static HTML Page
Learn how to add a favicon to static HTML pages, use correct paths and icon formats, and troubleshoot browser caching and local-file issues.
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
Apply CSS !important with jQuery .css()
Learn why jQuery .css() cannot set !important and how to use CSSStyleDeclaration.setProperty() to apply inline important styles safely.