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.
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.
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 .
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;
}
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
{
: inline-block;
}
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 |
| on parent with custom alignment |
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?
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
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers.
Get the Selected Radio Button Value with jQuery
Learn how to find which radio button is selected in jQuery and get its value with simple examples and common mistakes.