让整个表格居中对齐有多种方法,以下是完整的解决方案:
<div class="table-responsive">
<table class="table table-bordered mx-auto">
<!-- 表格内容 -->
</table>
</div>
<div class="text-center">
<table class="table table-bordered d-inline-block">
<!-- 表格内容 -->
</table>
</div>
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* 自定义样式 */
.centered-table-container {
display: flex;
justify-content: center;
margin: 20px 0;
}
/* 表格样式 */
.custom-table {
width: auto; /* 根据内容自动调整宽度 */
min-width: 300px; /* 最小宽度 */
}
</style>
</head>
<body>
<div class="container mt-5">
<h3 class="text-center mb-4">表格居中对齐示例</h3>
<!-- 方法1:使用 mx-auto -->
<div class="mb-5">
<h5>方法1:table + mx-auto</h5>
<table class="table table-bordered mx-auto custom-table">
<thead class="table-dark">
<tr>
<th>#</th>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>张三</td>
<td>25</td>
<td>北京</td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>30</td>
<td>上海</td>
</tr>
</tbody>
</table>
</div>
<!-- 方法2:使用 flex 容器 -->
<div class="mb-5">
<h5>方法2:flex 容器包裹</h5>
<div class="d-flex justify-content-center">
<table class="table table-striped table-hover custom-table">
<thead class="table-primary">
<tr>
<th>产品</th>
<th>价格</th>
<th>库存</th>
</tr>
</thead>
<tbody>
<tr>
<td>手机</td>
<td>¥2999</td>
<td>50</td>
</tr>
<tr>
<td>笔记本</td>
<td>¥5999</td>
<td>30</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- 方法3:网格系统 -->
<div class="mb-5">
<h5>方法3:网格系统控制</h5>
<div class="row justify-content-center">
<div class="col-12 col-md-8 col-lg-6">
<table class="table table-bordered">
<thead>
<tr>
<th>月份</th>
<th>收入</th>
<th>支出</th>
</tr>
</thead>
<tbody>
<tr>
<td>1月</td>
<td>¥50,000</td>
<td>¥30,000</td>
</tr>
<tr>
<td>2月</td>
<td>¥55,000</td>
<td>¥28,000</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
<div class="container">
<table class="table" style="width: 600px; margin: 0 auto;">
<!-- 固定宽度 + 自动边距 -->
</table>
</div>
<div class="container">
<div class="table-responsive-md">
<table class="table table-bordered mx-auto" style="max-width: 800px;">
<!-- 最大宽度限制 -->
</table>
</div>
</div>
<style>
.center-table-wrapper {
display: flex;
justify-content: center;
padding: 20px;
}
.center-table {
border-collapse: collapse;
width: auto;
margin: 0 auto;
}
.center-table th,
.center-table td {
border: 1px solid #ddd;
padding: 12px;
text-align: center;
}
.center-table thead {
background-color: #f8f9fa;
}
</style>
<div class="center-table-wrapper">
<table class="center-table">
<thead>
<tr>
<th>标题1</th>
<th>标题2</th>
</tr>
</thead>
<tbody>
<tr>
<td>内容1</td>
<td>内容2</td>
</tr>
</tbody>
</table>
</div>
<div class="d-flex justify-content-center align-items-center" style="min-height: 100vh;">
<table class="table table-bordered">
<!-- 表格在视口中完全居中 -->
</table>
</div>
<style>
.elegant-table {
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
border-radius: 10px;
overflow: hidden;
transition: transform 0.3s ease;
background: white;
}
.elegant-table:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0,0,0,0.15);
}
</style>
<div class="d-flex justify-content-center my-5">
<table class="table elegant-table" style="width: 70%;">
<!-- 优雅的表格 -->
</table>
</div>
<!-- 解决方案:响应式表格容器 -->
<div class="table-responsive">
<table class="table mx-auto" style="min-width: 600px;">
<!-- 小屏幕上会出现横向滚动条 -->
</table>
</div>
<!-- 解决方案:自动宽度 + 最大宽度 -->
<table class="table mx-auto" style="width: auto; max-width: 90%;">
<!-- 根据内容调整,但不超出容器 -->
</table>
<div class="row justify-content-center">
<div class="col-auto mb-4">
<table class="table table-sm">
<!-- 表格1 -->
</table>
</div>
<div class="col-auto mb-4">
<table class="table table-sm">
<!-- 表格2 -->
</table>
</div>
</div>
<div class="container">
<table class="table mx-auto">
<!-- 表格内容 -->
</table>
</div>
<div class="container text-center">
<h4 class="mb-3">数据表格</h4>
<table class="table table-bordered d-inline-block">
<thead>
<tr><th>标题</th><th>内容</th></tr>
</thead>
<tbody>
<tr><td>项目1</td><td>值1</td></tr>
</tbody>
</table>
</div>
<div class="d-flex justify-content-center">
<div class="card shadow" style="width: fit-content;">
<div class="card-body">
<table class="table mb-0">
<!-- 表格内容 -->
</table>
</div>
</div>
</div>
<!-- 各种工具类组合 -->
<table class="table
mx-auto <!-- 水平居中 -->
w-auto <!-- 宽度自适应 -->
shadow-sm <!-- 阴影 -->
rounded <!-- 圆角 -->
overflow-hidden"> <!-- 溢出隐藏 -->
<thead class="table-dark">
<!-- 表头 -->
</thead>
</table>
<!-- 不同屏幕尺寸下的居中策略 -->
<table class="table
mx-auto
ms-lg-0 <!-- 大屏幕上取消左边距 -->
me-lg-0"> <!-- 大屏幕上取消右边距 -->
<!-- 大屏幕靠左,小屏幕居中 -->
</table>
<!-- 不好:嵌套多层flex -->
<div class="d-flex justify-content-center">
<div class="d-flex">
<table>...</table>
</div>
</div>
<!-- 好:直接使用margin -->
<table class="mx-auto">...</table>
.center-table-container {
will-change: transform; /* 提示浏览器优化 */
}
最佳实践方案:
<table class="table mx-auto">
表格内容
</table>
<div class="d-flex justify-content-center">
<table class="table">
表格内容
</table>
</div>
<div class="row justify-content-center">
<div class="col-12 col-md-8">
<table class="table">
表格内容
</table>
</div>
</div>
关键点:
mx-auto 需要表格是块级元素或设置宽度table 类默认是 display: table,可以直接用 mx-auto一句话:在 Bootstrap 中,给表格添加 class="mx-auto" 是最简单的居中方法。