html - 水平对齐表格的CSS方法

标签 html css alignment css-tables

我想在浏览器窗口的中心显示一个固定宽度的表格。现在我用

<table width="200" align="center"> 

但 Visual Studio 2008 在此行给出警告:

属性“对齐”被认为已过时。建议使用更新的构造。

我应该对表格应用什么 CSS 样式以获得相同的布局?

最佳答案

Steven 是对的,在 theory :

the “correct” way to center a table using CSS. Conforming browsers ought to center tables if the left and right margins are equal. The simplest way to accomplish this is to set the left and right margins to “auto.” Thus, one might write in a style sheet:

table
{ 
    margin-left: auto;
    margin-right: auto;
}

但是本答案开头提到的文章为您提供了使表格居中的所有其他方法。

An elegant css cross-browser solution: This works in both MSIE 6 (Quirks and Standards), Mozilla, Opera and even Netscape 4.x without setting any explicit widths:

div.centered 
{
    text-align: center;
}

div.centered table 
{
    margin: 0 auto; 
    text-align: left;
}


<div class="centered">
    <table>
    …
    </table>
</div>

关于html - 水平对齐表格的CSS方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/301147/

相关文章:

javascript - 通过单击另一个表中的单元格删除表中的列

php - Codeigniter 表单验证 - 单独显示错误问题

javascript - 切换 div 可见性

alignment - OpenCL 中的快速 RGB => YUV 转换

android - 如何对齐textview和edittext?

javascript - Google Maps API 未加载?

html - 如何使边框半径框中的文本居中

javascript - 使用地理位置更改货币

html - 在 Css-grid 上设置 grid-gap 以自动填充可用的水平空间

css - 在 div 中对齐 <p>