Question
I have a layout like this:
<div>
<table>
</table>
</div>
I want the div to expand only to the width of the table, instead of taking up more horizontal space than its contents. How can I do that with CSS?
Short Answer
By the end of this page, you will understand why a div normally stretches to fill available width, and how to make it shrink to match its contents. You will learn practical CSS techniques such as display: inline-block, width: fit-content, and when each approach works best.
Concept
In CSS, a plain div is a block-level element by default. Block elements usually take up the full available width of their parent container, even if their content is much smaller.
That is why this happens:
<div>
<table></table>
</div>
Even if the table is narrow, the div still stretches across the container.
If you want the div to be only as wide as its contents, you need to change how the browser sizes it.
Common ways to do that are:
display: inline-block— makes the element behave like an inline-level box while still allowing width and heightwidth: fit-content— tells the browser to size the element based on its contentdisplay: inline-flexordisplay: inline-grid— useful in modern layouts when flexbox or grid is involved
This matters in real programming because layout sizing affects:
- wrappers around tables, buttons, badges, and cards
- tooltips, dropdowns, and menus
- content-based UI components
- avoiding unnecessary empty horizontal space
Mental Model
Think of a normal div like a shelf fixed to the full width of a wall. Even if you place a small object on it, the shelf still spans the whole wall.
If you change that div to something like inline-block, it becomes more like a small tray that grows only enough to hold what is placed inside it.
So:
- block
div= full-width shelf - inline-block
div= content-sized tray - fit-content = asking the browser to measure the tray based on what is inside it
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;
}
This is the most common beginner-friendly solution. The div will now shrink to match the width of the table.
Using fit-content
.wrapper {
width: fit-content;
}
This tells the browser to make the element just wide enough for its contents. This is clean and readable, but inline-block is still a very reliable option in many cases.
Full example
Step by Step Execution
Consider this example:
<div class="wrapper">
<table border="1">
<tr>
<td>A</td>
<td>B</td>
</tr>
</table>
</div>
.wrapper {
display: inline-block;
border: 1px solid red;
}
Here is what happens step by step:
- The browser reads the HTML and creates a
divcontaining atable. - By default, a
divwould bedisplay: block, so it would stretch to the full width of its parent. - The CSS changes
.wrappertodisplay: inline-block.
Real World Use Cases
This content-based sizing pattern is useful in many real interfaces:
- Wrapping a data table so a border or background only surrounds the table itself
- Badges and labels that should only be as wide as their text
- Dropdown menus that size to their menu items
- Tooltip containers that should fit their content neatly
- Small dashboard widgets that should not stretch unnecessarily
- Export or report sections where a framed box should match the actual content width
Example: a status badge
<div class="badge">Pending Review</div>
.badge {
display: inline-block;
padding: 4px 10px;
background: gold;
border-radius: 999px;
}
The badge width depends only on the text and padding, not the full line width.
Real Codebase Usage
In real projects, developers often use content-sized containers in a few common patterns:
Wrapping visual components
A wrapper may add:
- border n- shadow
- padding
- background color
while the inner content controls the width.
.card-like-wrapper {
display: inline-block;
padding: 12px;
border: 1px solid #ccc;
background: white;
}
Using guard styles for reusable components
Teams often create utility classes such as:
.fit-content {
display: inline-block;
}
Then reuse them across components.
Combining with overflow handling
If content might become too wide, developers may combine sizing with scroll behavior:
.table-wrapper {
display: inline-block;
max-width: 100%;
overflow-x: auto;
}
Using layout-specific alternatives
In flex or grid layouts, developers may prefer:
Common Mistakes
1. Expecting a normal div to shrink automatically
Broken expectation:
<div class="wrapper">
<table></table>
</div>
.wrapper {
border: 1px solid black;
}
A plain div is block-level, so it usually stretches full width.
Fix
.wrapper {
display: inline-block;
}
2. Setting width: auto and expecting content width
.wrapper {
width: auto;
}
Beginners often think auto means “match content.” For block elements, it usually means “use available width,” not “shrink-wrap the contents.”
Fix
Use:
Comparisons
| Approach | What it does | Good for | Notes |
|---|---|---|---|
display: block | Takes full available width | Standard page sections | Default for div |
display: inline | Flows like text | Small inline content | Width/height control is limited |
display: inline-block | Sizes to content while keeping box behavior | Wrappers, badges, tables | Very practical and common |
width: fit-content | Sizes width to content | Modern CSS layouts | Clear intent, but parent layout still matters |
display: inline-flex |
Cheat Sheet
/* Most common fix */
.wrapper {
display: inline-block;
}
/* Alternative */
.wrapper {
width: fit-content;
}
/* If using flex behavior too */
.wrapper {
display: inline-flex;
}
Quick rules
- A normal
divisdisplay: block. - Block elements usually stretch to fill available width.
- Use
inline-blockwhen you want adivto size to its content. - Use
fit-contentwhen you want explicit content-based sizing. - Check parent flex/grid styles if sizing seems wrong.
Good default answer
div {
display: inline-block;
}
Common edge cases
- Parent flex or grid containers may affect sizing.
- Tables with fixed width will still force the wrapper wider.
width: autodoes not usually mean content width for block elements.
FAQ
Why does a div take the full width by default?
Because a div is a block-level element, and block elements usually expand to fill the width of their parent.
How do I make a div fit its content width?
The most common solution is:
.wrapper {
display: inline-block;
}
Is width: fit-content better than inline-block?
Not always. fit-content is expressive and useful, but inline-block is simple and works well for many wrapper cases.
Will this work for content other than tables?
Yes. It also works for text, images, buttons, badges, forms, and other elements.
Why is my wrapper still stretching inside flexbox?
Because flex container rules can affect item sizing. Check properties like align-items, flex, and align-self.
Can I use this to wrap a border tightly around a table?
Yes. That is one of the most common reasons to use display: inline-block on the wrapper.
Mini Project
Description
Build a small table wrapper component that displays a bordered box around a table and makes the box only as wide as the table itself. This demonstrates content-based sizing and is useful for reports, admin panels, and compact UI sections.
Goal
Create a wrapper around a table that shrinks to the table’s width instead of stretching across the full page.
Requirements
- Create an HTML page with a
divwrapping atable - Add visible border and padding to the wrapper
- Make the wrapper only as wide as the table contents
- Add at least two rows of sample table data
- Keep the solution in plain HTML and CSS
Keep learning
Related questions
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.
CSS Parent Selector Explained: Selecting a Parent <li> from an <a>
Learn whether CSS has a parent selector, how :has() works, and practical alternatives for styling a parent li from a child anchor.
CSS Previous Sibling Selector: What Exists and How to Work Around It
Learn whether CSS has a previous sibling selector, why it does not, and practical ways to style earlier elements using CSS alternatives.