html - 将表格标题与行对齐

标签 html css

我正在尝试创建具有固定行标题的表格(因此在滚动列标题期间保持不变),我已经能够实现但现在表格标题未对齐 到下面的行。我还创建了一个 jsfiddle为了那个原因。 目前的输出是这样的:

enter image description here

这是提取的 HTML 示例。

<head>
    <style>
        table
        {
            /*background-color: #aaa;*/
            width: 800px;
        }
        tbody
        {
            /*background-color: #ddd;*/
            height: 200px;
            width: 800px;
            overflow: auto;
        }
        td
        {
        }

        thead > tr, tbody
        {
            display: block;
        }
    </style>
</head>
   <body>
    <div id="containerdiv" style="width: 800px">
        <table id="dynamictable">
            <thead>
                <tr>
                    <th>
                        ID
                    </th>
                    <th>
                        Title
                    </th>
                    <th>
                        Media Title
                    </th>
                    <th>
                        Broadcast Time
                    </th>
                    <th>
                        Destination
                    </th>
                    <th>
                        Channel
                    </th>
                    <th>
                        Start Time
                    </th>
                    <th>
                        End Time
                    </th>
                </tr>
            </thead>
            <tbody id="tablebody">
                <tr id="0">
                    <td>
                        ID: 0
                    </td>
                    <td>
                        Some Content
                    </td>
                    <td>
                        Some more contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                </tr>
                <tr id="1">
                    <td >
                        ID: 1
                    </td>
                    <td>
                        Some Content
                    </td>
                    <td>
                        Some more contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                </tr>
                <tr id="Tr1">
                    <td>
                        ID: 1
                    </td>
                    <td>
                        Some Content
                    </td>
                    <td>
                        Some more contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                    <td>
                        Some more contents contents contents
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

最佳答案

不要设置 thead > tr, tbody显示: block 。如果您删除该行,它应该可以工作。

默认情况下,<tbody>display:table-row-group<tr>display:table-row .您希望将其保留为这些默认值。

关于html - 将表格标题与行对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18600537/

相关文章:

javascript - 捕获右键单击 HTML DIV

javascript - jQuery wrap 函数 wrap 工作起来很奇怪

html - 在浏览器调整大小时,大图像不会缩小到 css 网格容器

HTML、CSS 将 DIV 中的文本与平行 div 中的文本对齐

javascript - 使用过渡的 div 动画

Jquery 日期选择器 : Show multiple months (custom number) in multiple lines

javascript - 带有图像的下拉菜单(没有像 jquery 这样的库)

html - 如何在响应式布局中将元素定位在背景图像上

javascript - 如果 CSS3 过渡不存在,使用 Modernizr 和 jQuery 来制作动画

css - 如何使用 React Native Animate 将 view flex 从 0 更改为 1