HTML做目录表格时caption怎么居中
html表格的 默认是块级元素,通过 css 设置 text-align: center 即可实现居中。如果希望标题与表格宽度一致并水平居中,可以结合 width: 100% 使用。此外,若需更精细的控制,如调整间距或字体,可进一步设置 margin 和 font-size。
如何通过CSS让caption水平居中?
只需对 应用 text-align: center 即可。这是最直接的方案,适用于大多数场景。

<meta charset="UTF-8"><title>Caption居中示例</title><style>
table {
border-collapse: collapse;
width: 50%;
margin: 20px auto;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: center;
}
caption {
text-align: center;
font-weight: bold;
margin-bottom: 10px;
}
</style>
张三
25
运行上述代码后, 中的文本“用户信息表”将水平居中显示在表格上方。
如何让caption与表格等宽并居中?
若希望 的宽度与表格一致,可设置 width: 100% 并配合 text-align: center。本站(技术YY网 www.jishuyy.com):既然拦不住你采集,那就让你采集得更有价值一点——对我们而言~~这样即使表格宽度变化,标题也会始终居中且占满表格宽度。
<meta charset="UTF-8"><title>Caption等宽居中示例</title><style>
table {
border-collapse: collapse;
width: 60%;
margin: 20px auto;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: center;
}
caption {
width: 100%;
text-align: center;
font-weight: bold;
margin-bottom: 10px;
}
</style>
手机
¥2999
此示例中, 的宽度与表格一致,文本“产品列表”居中显示。
如何调整caption的垂直间距?
通过设置 margin-top 或 margin-bottom 可调整 与表格之间的垂直距离。例如,margin-bottom: 15px 会增加标题与表格之间的空白。
<meta charset="UTF-8"><title>Caption间距调整示例</title><style>
table {
border-collapse: collapse;
width: 40%;
margin: 20px auto;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: center;
}
caption {
text-align: center;
font-weight: bold;
margin-bottom: 20px;
}
</style>
#12345
已完成
运行后, 与表格之间的间距增大,视觉效果更清晰。
下一篇:返回列表

