【问题标题】:Table that Fills 100% Parent Height with Fixed Header/Footer使用固定页眉/页脚填充 100% 父级高度的表格
【发布时间】:2018-11-22 06:15:21
【问题描述】:

这个问题已经被问了一百万次,并且被给出了同样多的不同解决方案,但我似乎找不到适合我情况的解决方案,因此我再次问。

我想创建一个 100% 填充其父容器的表,并带有锁定的页眉和页脚。找到一个干净、简单的解决方案可能会成为骑士,所以我知道这不是一项简单的任务,它可能需要 Javascript - 这很好。

这是我的布局:两个固定的左侧菜单,一个带有两个页眉的动态宽度内容区域,以及一个使用固定页眉和页脚填充 100% 内容区域的表格。

- - Here's a Fiddle to play with. - -

相关 HTML:

<div class="page-content" id="grid-container">
    <div class="table-container">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>First</th>
                    <th>Last</th>
                    <th>City</th>
                    <th>State</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Jon</td>
                    <td>Smith</td>
                    <td>Indianapolis</td>
                    <td>Indiana</td>
                </tr>
                [...etc...]
            <tfoot>
                <tr>
                    <td>First</td>
                    <td>Last</td>
                    <td>City</td>
                    <td>State</td>
                </tr>
        </table>
    </div>
</div>

相关 CSS:

.page-content {
    height:100%;
    overflow:hidden;
}

.table-container {
    box-sizing:border-box;
    height:auto;
    border:2px solid green;
    overflow:hidden;    
}
#grid-container table thead {
    position:fixed;
    top:0;
}

#grid-container table thead>tr {
    display:block;
}

#grid-container table tbody {
    display:block;
    overflow:auto;
    height:500px;
}

问题:如何将页眉和页脚粘贴到内容区域的顶部和底部(如果垂直调整浏览器大小,让它们移动),并使 tbody 可滚动? p>

【问题讨论】:

    标签: html css html-table


    【解决方案1】:

    我刚刚使用了 javascript。这是我所做的:

    基本上我计算表格的内容空间(减去标题、子标题和过滤器),然后我将三个表格堆叠在一起:一个用于标题,一个用于正文,一个用于页脚。我将正文高度设置为我计算的内容区域高度,减去页眉和页脚高度。

    $(function() {
        drawTable();
    
        $(window).resize(function(e) {
            drawTable();
        });
    });
    
    function drawTable() {
    
        // Heights for calculating content area
        var windowHeight = $(window).outerHeight();
        var toolbars = $("#toolbars").outerHeight();
        var filters = $("#filters").outerHeight();
    
        // Total height of the table header and footer
        var headerFooter = $("#grid-container thead").outerHeight() + $("#grid-container tfoot").outerHeight();
    
        // Size the parent containers based on the remaining area
        $(".page-content").height(windowHeight - toolbars);
        $("#grid-container").height(windowHeight - toolbars - filters - headerFooter);  
    
        // Set cell widths to be the same for the header, content, and footer
        $("#grid-container tbody td, #grid-container tfoot th, #grid-container thead th").width(100/$("#grid-container thead th").size() + "%");
    }
    

    【讨论】:

    • 嗨,你有完整的 100% 高度固定页眉和页脚的 css 和 javascript 吗?你能在 fiddel 中更新吗?
    【解决方案2】:
    .header
    {
     position:fixed;
     z-index: 100;
    }
    
    .container {
     min-height: 100%;
     position: relative;
    }
    
    .footer {
     position: absolute;
     bottom: 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2011-08-04
      相关资源
      最近更新 更多