HTML 表 : THEAD width depending on TBODY in Chrome

标签 html css html-table cross-browser width

经过一些测试后,我发现 Chrome 不会像 Opera 那样根据 TBODY 元素计算 THEAD 列宽。有没有办法避免在 thead 中指定它?示例:

<!DOCTYPE html>
<html>
<head>
    <style type="text/css" media="all">
    table{
        width:800px;
        border:1px solid #CCCCCC;
        table-layout: fixed; 
        border-spacing:0px;
        box-sizing: border-box;
    }

     table td.option{
        width:100px; 
    }

    table td {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        border:1px solid #EEEEEE;
    }

    table td.active{
        text-align:center;
        width:100px;
    }


   td.thead{
       /* something that makes that width: is depending on the total width of the tbody elements */
   }

    table td.nonfixed{
        width:100%;
    }      
    </style>
    <title>Untitled</title>
</head>
<body>
<table>
    <thead>
        <tr>
            <td class="thead">Name</td>
            <td class="thead">Description</td>
            <td class="thead">Active</td>
            <td class="thead" colspan="2">Options</td>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="100"><a href="">+ Add new row</a></td>
        </tr>
    </tfoot>
    <tbody>
        <tr>
            <td class="nonfixed">[Name 1]</td>
            <td class="nonfixed">[Description 1]</td>
            <td class="active">[X]</td>
            <td class="option">Edit</td>
            <td class="option">Delete</td>
        </tr>
        <tr>
            <td class="nonfixed">[Name 2]</td>
            <td class="nonfixed">[Description 2]</td>
            <td class="active">[0]</td>
            <td class="option">Edit</td>
            <td class="option">Delete</td>
        </tr>
    </tbody>
</table>
</body>
</html>

最佳答案

问题通过添加 <colgroup> 得到解决。元素。 Chrome 使用第一行(tr 或 colgroup)来确定 td 宽度。此解决方案在旧版本的 IE 中遇到一些问题,不支持带百分比的 td 宽度。

关于HTML 表 : THEAD width depending on TBODY in Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14444436/

相关文章:

css - 如何在一行中创建多行表格(Material-UI/CSS)

html - 连续html中的一些行

javascript - 仅在 vimeo 视频上隐藏喜欢、观看和分享

jquery - 通过缓慢滑动到侧面动画隐藏单击时的 div

css - 将多个字体文件合并到一个 @font-face 中

css - 当 colspan 设置在下面的 td 行时,单个 th 的边界扩展到相邻的 th

javascript - 哪个建议最适合根据用户视口(viewport)调整布局?

html - 是否有具有多种 CSS 样式的标准 HTML 布局?

javascript - 将 css 应用于旋转木马幻灯片上的另一个跨度

html - 溢出-x :hidden "cuts-off" dropdown