Centering a table with CSS is one of those tasks that looks trivial until you actually try it. The reason is simple but easy to miss: a <table> element is not a normal block-level box. Think about it: you add text-align: center to the parent, hit refresh, and… nothing happens. Because of that, by default it behaves as display: table, which shrinks to fit its content and ignores the centering tricks you'd normally use on a <div>. Now, the table stubbornly hugs the left edge of the page. Once you understand that quirk, centering tables becomes predictable — and you gain several reliable techniques you can apply depending on your layout Not complicated — just consistent. No workaround needed..
Below you'll find every practical method for centering a table horizontally and vertically, how to center the content inside the table, and the mistakes that break centering in real projects.
Why Centering a Table Behaves Differently
A <table> with default styling has display: table, which is a block-level box in the sense that it sits on its own line, but its width is determined by its content rather than filling the container. That means:
text-align: centeron the parent does not center adisplay: tableelement.margin: 0 autodoes work, but only if the table has a defined width smaller than its container.width: 100%makes the table fill the container, so centering becomes visually meaningless.
The core rule to remember: a table can only be centered if it is narrower than its parent container. If the table is already as wide as the space available, there is nothing left to center.
Method 1: The Classic margin: 0 auto Approach
This is the most widely supported and simplest solution. Give the table a width and let the browser distribute the remaining space equally on both sides And that's really what it comes down to..
table {
width: 600px;
margin-left: auto;
margin-right: auto;
}
Or in shorthand:
table {
width: 600px;
margin: 0 auto;
}
Why it works: When both left and right margins are set to auto on a block-level element with a fixed width, the browser splits the leftover space evenly. Since display: table is treated as block-level for margin purposes, this behaves exactly like centering a <div>.
Best for: Static layouts, email templates, and situations where you need maximum browser compatibility. This method works in every browser released in the last two decades.
Watch out for: Forgetting the width. Without a width, the table shrinks to fit its content and margin: auto has no effect because there's no leftover space to distribute.
Method 2: text-align: center with display: inline-table
If you prefer to control centering from the parent element, you can change the table's display type so it behaves like an inline element.
.table-wrapper {
text-align: center;
}
.table-wrapper table {
display: inline-table;
}
Because inline-table makes the table participate in inline formatting, the parent's text-align: center now applies to it — the same way it centers an image or a <span> Most people skip this — try not to..
Best for: Quick centering inside a wrapper, or when you're already using text-align on the container.
Watch out for: text-align: center is inherited, so all the text inside your table cells will also become centered. Fix that by resetting it:
.table-wrapper table {
display: inline-table;
text-align: left;
}
Method 3: Flexbox on the Parent
Flexbox gives you the most control and reads clearly in modern codebases.
.table-wrapper {
display: flex;
justify-content: center;
}
The wrapper becomes a flex container, and justify-content: center places the table in the middle of the main axis. You don't even need to set a width on the table — it will shrink to fit its content and sit centered.
It sounds simple, but the gap is usually here.
To center both horizontally and vertically:
.table-wrapper {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
Best for: Modern layouts, dashboards, modals, and any situation where you also need vertical centering.
Watch out for: Flex items can shrink unexpectedly. If your table has many columns, add flex-shrink: 0 or set a min-width so it doesn't get squeezed.
Method 4: CSS Grid
Grid offers the shortest syntax for perfect centering And that's really what it comes down to..
.table-wrapper {
display: grid;
place-items: center;
}
place-items: center is shorthand for align-items: center and justify-items: center, centering the table on both axes in a single line.
If you only want horizontal centering:
.table-wrapper {
display: grid;
justify-content: center;
}
Best for: Clean, modern CSS where you want minimal code. Grid is supported in all current browsers.
Method 5: Absolute Positioning with Transform
When the table must be centered relative to the viewport or a positioned container regardless of surrounding content, absolute positioning works well.
.table-wrapper {
position: relative;
height: 400px;
}
.table-wrapper table {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
The top and left values move the table's top-left corner to the center point, and translate(-50%, -50%) pulls it back by half its own width and height, producing true centering.
Best for: Overlays, print layouts, and legacy codebases where flexbox isn't available.
Watch out for: The table is removed from normal flow, so it won't push other content around. Make sure the wrapper has a defined height The details matter here..
Centering the Content Inside the Table
Centering the table itself is only half the job. Readers often mean "center the text in my cells." That's a different property set.
Horizontal text alignment:
td, th {
text-align: center;
}
Vertical alignment:
td, th {
vertical-align: middle;
}
Centering everything at once:
table {
margin: 0 auto;
border-collapse: collapse;
}
th, td {
text-align: center;
vertical-align: middle;
padding: 12px 16px;
}
Note that vertical-align on table cells is well supported, unlike vertical-align on block elements, which only affects inline content And that's really what it comes down to..
Common Mistakes That Break Table Centering
- No width set.
margin: 0 autoneeds a width smaller than the container to work. width: 100%on the table. A full-width table is already centered; adding margin rules changes nothing visually.- **Using `
text-align: center on the table itself.In practice, ** This property centers inline content within cells, not the table element. To center the table box, pair a defined width with margin: 0 auto, or use a layout method like flexbox or grid.
- Nested wrappers without explicit sizing. Centering a table inside a flex or grid container works only if the wrapper itself is centered or sized correctly; otherwise the table may appear offset even when the cell content is aligned.
Conclusion
Centering an HTML table depends on what you're trying to center and the layout context you're working in Simple, but easy to overlook..
For a simple, fixed-width table in standard document flow, `