Why does content overflow my HTML table cell even with a fixed width?
By default a table sizes its columns to fit their content, so a width on a cell is only a hint. A long word or web address can push the column wider or spill out of it. Set table-layout to fixed, give the table a width, and tell long text where it may break.
Cite
CoursesWeb Forum, "Why does content overflow my HTML table cell even with a fixed width?", https://coursesweb.net/forum/t/why-does-content-overflow-my-html-table-cell-even-with-a-fixed-width/, accessed 2026-11-11.- Teodora VanceStaffModerator: HTML, CSS and front end JavaScript ·
A minimal working example
Here is a table with one long address that has no spaces in it, the usual cause of this problem:
<table class="data"> <thead> <tr><th>File</th><th>Source</th></tr> </thead> <tbody> <tr> <td>report.pdf</td> <td>https://example.com/a-very-long-address-with-no-spaces/that-keeps-going-and-going</td> </tr> </tbody> </table>.data { table-layout: fixed; width: 100%; max-width: 600px; border-collapse: collapse; } .data th:first-child { width: 8rem; } .data th, .data td { border: 1px solid #c5c6ce; padding: 0.5rem; overflow-wrap: anywhere; }The first column is 8rem wide. The second column gets the rest of the 600px, and the long address wraps inside it instead of pushing the table wider.
Why this works
A table has two ways to calculate column widths, chosen by the
table-layoutproperty. MDN's table-layout page describes them:Value What the browser does auto (the default) Adjusts the table and cell widths to fit the content fixed Uses the table's width and the widths you set, and ignores the content MDN also says that with
fixed, the table width needs to be specified explicitly, or the browser quietly falls back to the automatic algorithm. And for the fixed algorithm, a column gets its width from acolelement, or otherwise from a cell in the first row that has an explicit width, or otherwise from the shared remaining space. That is why the example sets the width on the header cell in the first row.Fixed layout has a side effect. MDN notes that later cell content might not fit in the column widths provided. So a long unbroken string can now spill out of its cell instead of widening the column. That is where
overflow-wrapcomes in. MDN's overflow-wrap page explains that it sets whether the browser may insert line breaks inside an otherwise unbreakable string, such as a long word or a URL.Which value should you pick? Both
anywhereandbreak-wordallow a break at any point when a word does not fit. The difference is thatanywherealso counts those break points when the browser works out the minimum width of a cell. In a table that is still on the automatic layout,anywhereis the one that lets the cell shrink. Withtable-layout: fixed, either one works.Mistakes beginners make
- Setting width only on the td. With the default automatic layout, the browser treats it as a suggestion. When the content cannot fit, the column grows. MDN says that with
auto, the table would grow to accommodate its contents, despite the specified width. - Using word-break: break-all on everything. It splits ordinary words in the middle.
overflow-wraponly breaks a word when that word cannot fit on its own line, so normal text still wraps at spaces. - Leaving white-space: nowrap or a pre element in the cell. These tell the browser never to wrap, so no amount of width helps. Remove them or give that one cell its own rule.
If you would rather cut long text short than wrap it, MDN's example uses a fixed layout with
overflow: hidden,white-space: nowrapandtext-overflow: ellipsison the cells. The cell then shows an ellipsis (three dots) where the text is cut off.Check this in order
- Does the table itself have a width, in CSS or a
widthattribute? Without one, fixed layout has no effect. - Is
table-layout: fixedapplied to the table element? Look at the Computed tab in DevTools. - Is the width set on a first-row cell or a
col? - Is there a long string with no spaces in the cell? Add
overflow-wrap: anywhere. - Is there
nowrapor apreelement or an image wider than the column? For images, addmax-width: 100%. - On a phone, does the whole table still not fit? Put it inside a div with
overflow-x: autoso it scrolls sideways.
Tables are for tabular data. If you were using one to put page content in columns, flexbox is easier: see how to put two divs side by side.
- Setting width only on the td. With the default automatic layout, the browser treats it as a suggestion. When the content cannot fit, the column grows. MDN says that with
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.