html - 为什么我的 Chrome 表格单元格宽度错误?

标签 html google-chrome layout width html-table

我有一个包含两行的表格。第一行只有三个单元格。第二行有两个单元格,第一个单元格包含另一个需要填充整个单元格的表格。

<table border="1" style="border-collapse:collapse;">
    <tr>
        <td style="WIDTH: 205px;">1</td> <!--This width doesn't apply in Chrome-->
        <td style="width:100%;">2</td>
        <td style="WIDTH: 5px;">3</td>
    </tr>                

    <tr>
        <td colspan="2">

                <TABLE width="100%" border="1" style="border-collapse:collapse;table-layout: fixed;">
                    <TR>
                        <TD style="width:130px;">
                            A</TD>
                        <TD style="width:90px;">
                            B</TD>
                        <TD style="width:230px;">
                           C</TD>
                    </TR>

                </TABLE>        
        </td>
        <td>
            D
        </td>
    </tr>
</table>  

很简单,真的……我是这么想的。

它在 IE 中的显示符合我的预期。但是 Chrome 似乎没有正确应用第一个单元格的宽度。它似乎受到下面单元格中表格的影响。

enter image description here

为什么会发生这种情况,我该如何解决?

最佳答案

你应该做的两件事:

  • 在表格元素上,使用table-layout: fixed;
  • 插入列并给它们一个宽度
    • (您还可以为第一行的表头/单元格分配宽度)

像这样:

<table border="1" style="table-layout: fixed; width: 100%;">
    <colgroup>
        <col style="width: 205px;">
        <col style="width: auto;">
          <!-- Use "width: auto;" to apply the remaining (unused) space -->
        <col style="width: 5px">
    </colgroup>

    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr> 

    <!-- Etc. -->

关于html - 为什么我的 Chrome 表格单元格宽度错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12212608/

相关文章:

css - 在 chrome 中为下拉菜单设置字体大小似乎不会影响样式

css - 与 Chrome 相比,Firefox 忽略了 SVG 文本中的多个空格并将它们显示得更小

javascript - 使用javascript错误在另一个div中显示菜单

c# - Blazor 事件回调问题父/子组件 "does not have a property matching the name ' OnClickCallback'"

jquery - 如何在 `drop` 事件后停止重定向?

Android LinearLayout 选择器背景色

android - 我怎样才能给小部件的宽度一个百分比而不是一个明确的值?

php - 显示从数据库到 Bootstrap 轮播的多个图像

html - CSS 3 渐变不适用于 chrome/firefox

java - 使布局适用于所有 SDK<11?