html - 防止div垂直展开,给它滚动条

标签 html css

引用以下 fiddle :https://jsfiddle.net/537w3qsn/

这是我想要的:

  • 页眉和页脚应始终在页面上保持可见。
  • 主体(中间的绿色 div)应该有一个垂直滚动条,否则会导致内容溢出。它不应增长太多并将页脚推离页面。
  • 布局应该流畅。模态应占据整个屏幕。
  • 请仅在万不得已时才使用 javascript。
  • 否则,您可以随意修改 HTML 和 CSS,但您希望达到相同的效果。

示例 CSS:

.modal {
    padding: 0px;
    background-color: #ff6666;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

.body {
    overflow: auto;
    background-color: #b3ff66;
}

.dialog {
    width: 100%;
    height: 100%;
    padding: 0px;
    margin: 0px;
    background-color: #66ffff;
}

.content {
    height: 100%;
    border-radius: 0px;
    background-color: #b266ff;
}

示例 HTML:

<div class="modal">
    <div class="dialog">
        <div class="content">
            <div class="header">
                <h4>Header</h4>
            </div>
            <div class="body">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Test</th>
                            <th>Test</th>
                            <th>Test</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- LOTS OF CONTENT HERE -->
                    </tbody>
                </table>
            </div>
            <div class="footer">
                This is the footer.
            </div>
        </div>
    </div>
</div>

最佳答案

您可以使用 flexbox 来分配高度。

/* Create a vertical flexible container */
.modal {
  display: flex;
  flex-direction: column;
}

/* Every child will fit their content, but .body will take the remaining space */
.body {
  flex: 1;
}

您可以在此处查看示例:https://jsfiddle.net/tzi/ud4zsn2e/

关于html - 防止div垂直展开,给它滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120539/

相关文章:

HTML 5 Doctype 导致 quirksmode?

javascript - 我怎样才能知道html元素的宽度和高度?

jQuery .css( {'-moz-animation-delay' :'300ms' }) 在 Firefox 上不工作

html - 在 div 内使用特定类设置段落样式

CSS 具有已知子宽度的两列

html - 使用 xpath 从格式不正确的页面解析 XML

html - Bootstrap 3字形图标在悬停时显示文本

javascript - 移动环境中的网站桌面模式

html - 拉伸(stretch)图像

html - 使用 input-groups bootstrap 创建一个内联表单