简体   繁体   English

HTML表格单元格未正确对齐

[英]HTML table cells not properly aligned

I have the following HTML table: 我有以下HTML表:

<table style="width: 100%;">
<tr>
    <td class="title_bar_left_border"></td>
    <td class="title_bar_middle"></td>
    <td class="title_bar_right_border"></td>
</tr>
</table>

With the following css rules: 使用以下CSS规则:

.title_bar_left_border
{
    BACKGROUND-IMAGE: url(tray_left.gif);
    WIDTH:  3px;
    HEIGHT: 24px;
}

.title_bar_right_border
{
    BACKGROUND-IMAGE: url(tray_right.gif);
    WIDTH:  3px;
    HEIGHT: 24px;
}

.title_bar_middle
{
    BACKGROUND-IMAGE: url(tray_middle.gif);
    WIDTH: 100%;
    BACKGROUND-REPEAT: repeat-x;
    HEIGHT: 24px;
}

Any idea why this is the result? 知道为什么会这样吗?

替代文字

Instead of getting a nice table header with rounded corners you get this weird gap between the cells. 而不是获得带有圆角的漂亮表头,而是得到单元格之间的怪异间隙。 Where are the gaps coming from? 差距从何而来? Besides fixing this ugly issue, I would like to understand the rationale as to why all browsers render the HTML this way. 除了解决这个丑陋的问题之外,我还想了解为什么所有浏览器都以这种方式呈现HTML的基本原理。

Those bars are cell spacing and padding. 这些条是单元格间距和填充。

You need to set the background of the division to the color of the gif files. 您需要将分割的背景设置为gif文件的颜色。 That white space is the padding around the cells. 白色空间是细胞周围的填充物。

You can also set cellpadding='0' and cellspacing='0' in the table definition. 您还可以在表定义中设置cellpadding ='0'和cellspacing ='0'。 The default values are 1 and 2 respectively. 默认值分别为1和2。 I'd recommend against it, though, as it might cause issues with the data bearing rows. 不过,我还是建议不要这样做,因为它可能会导致数据承载行出现问题。

You see the default spacing. 您会看到默认间距。 Fix: 固定:

table
{
    border-collapse:  collapse;
    border-spacing:   0;
}
table{border-collapse:collapse;}

需要删除表格的边框...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM