如何使用CSS定位固定的可变高度标题和可滚动的内容框?

Dav*_*Roe 50 css

我正在尝试制作一个带有固定标题和可滚动内容区域的网页.当标题具有已知高度时,这是微不足道的,但是当标题流畅时,我正在努力寻找解决方案.

我想要的布局是:

--------------
head
--------------
content
--------------
Run Code Online (Sandbox Code Playgroud)

其中"head"是其内容需要的高度,"content"没有最小高度,但在变为可滚动之前将达到视口底部的最大高度.

这些天在纯CSS中这可能吗?我的目标是IE8 +.

为了澄清我想要的东西,如果我知道标题的高度,我会怎么做:

<!DOCTYPE html>
<html>
    <head>
        <style type="text/css">

body {
    margin: 0;
}

#head {
    background: yellow;
    height: 20px; /* I can't rely on knowing this. */
}

#content {
    background: red;
    position: absolute;
    top: 20px; /* here also */
    bottom: 0;
    width: 100%;
    overflow: auto;
}

        </style>
    </head>
    <body>
        <div id="head">some variable height content</div>
        <div id="content">
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
            scrollable content<br/>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Sha*_*una 24

假设你的意思是"固定" position:fixed,我认为在纯CSS中不可能position:fixed将元素从文档流中取出.

但是,它应该只需要一两行JavaScript来获得你想要的东西.像这样的东西(未经测试,仅用于示例目的,将需要语法调整实际工作):

var height = document.getElementById("head").offsetHeight;
document.getElementById("content").style.marginTop = height + 'px';
Run Code Online (Sandbox Code Playgroud)

这样的东西应该可以获得固定的渲染高度,<div>并相应地设置内容<div>的边距.您还需要在固定上明确设置背景颜色<div>,否则滚动时内容将显示为固定的内容.

  • 调整页面大小后不要忘记重新计算 (7认同)

Eri*_*ric 21

这是一个解决方案,但这是一个骗局.基本上,你有一个重复的标题元素,在固定位置下按下内容:

<div class="outer">
    <div class="header">Header content goes here</div>
    <div class="header-push">Header content goes here</div>
    <div class="content">
        ...
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 我已经尝试了几个小时来提出纯CSS解决方案,并得出结论,确实,这是不可能的.96年发明的标准的奇迹. (2认同)
  • @WoodrowBarlow这不会使网站无法使用.标题只会滚动,而不是保持固定. (2认同)

gec*_*eca 9

我把接受和Eric的答案结合起来.空div用于推动"head"下面的内容.当触发window.onresize时,此div的宽度由jQuery设置:

function resizeHeader() {
    $(".header-push").height($(".header").height());
}
$(document).ready(resizeHeader);
$(window).resize(resizeHeader);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此jsFiddle.

  • 奇怪的是,我最喜欢这种方法.它不需要重复的标题(但它确实需要一个空的div,这不是我的两个),并且它不要求内容知道有关标题的任何信息.它也短得多. (2认同)
  • 在窗口调整大小期间添加一点延迟以避免太多触发器很有帮助 - 我使用 250 毫秒,看起来足够好:http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-调整大小完成后触发 (2认同)

Dim*_*htz 6

最简单的方法就是简单地替换position: fixed;position: sticky;

header {
  position: sticky;
  top:0;
  background-color: red;
  color: #fff;
}

main {
  background-color: green;
  color: #fff;
  min-height: 150vh;
}
Run Code Online (Sandbox Code Playgroud)

演示: https: //jsfiddle.net/4zndve6p/