css - 是否有一种 CSS 方法可以使 div 在顶部和底部的 2 个固定边距之间垂直居中?

标签 css vertical-alignment

如果可能,我如何才能仅使用 CSS 使 div 在顶部和底部的固定边距之间垂直居中?边距用于容纳页眉和页脚。

http://jsfiddle.net/u8PhW/6/

<div id="header" style="position: fixed; height: 200px; background-color: orange;"></div>
<div class="viewport" style="overflow: hidden">
    <div class="page">
        <div class="content">Vertically Aligned Section 1</div>
    </div>
    <div class="page">
        <div class="content">Vertically Aligned Section 2</div>
    </div>
</div>
<div id="footer" style="position: fixed; height: 50px; bottom: 0px; background-color: gray;"></div>

这个 HTML 的想法是有一个固定的页眉和页脚,内容应该在这两个 div 之间居中。此外,javascript 允许“跳转”到不同的页面。这些页面中的内容应位于页眉和页脚之间。

最佳答案

Demo

CSS

div.content {
    position: fixed;
    background-color: red;
    top:200px;
    bottom:50px;
    margin:auto;
    height: 200px;
}

关于css - 是否有一种 CSS 方法可以使 div 在顶部和底部的 2 个固定边距之间垂直居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23335543/

相关文章:

html - 垂直对齐 div 中的最后一个元素

angularjs - 如何在网格布局中将 bootstrap 下拉菜单和其他菜单对齐?

javascript - 根据所使用的字段而变化的变量

html - 内容可滚动时具有固定位置的页眉和页脚?

html - 如何向具有绝对子项的相对容器添加填充或边距

css - 垂直对齐头痛

css - 将内联 block 的底部与文本底部对齐(不包括下行)

html - 使用 css 渐变的 Angular 网格图案

html - 相对于图像高度垂直对齐多个 <p>

html - 垂直对齐 anchor 内的图像