HTML -> 固定顶部、固定底部和动态内容

标签 html css layout

我想创建一个页面,其中:

1) 顶部,100px,固定 2)底部,100px,固定 3)顶部和底部之间有动态内容

我能够准确地创建它,但我有一个问题,当我滚动动态内容时,元素在顶部和底部下方和上方,并且通过固定的顶部/底部可以看到相同的元素。

我尝试清楚:两者;位于顶部和底部 div,但由于它们是固定的,所以清楚:两者;没用。

我想将动态元素保留在顶部和底部之间的内容 div 内,并且如果可能的话仍使用主文档滚动条在其中滚动。

这里是一个例子,假设线条代表顶部和底部的固定div,“元素”是动态内容。

元素 - 不正确


元素 - 不正确


元素

元素

元素

元素

元素


元素 - 不正确


元素 - 不正确

希望我说得足够清楚,并提前致谢!

最佳答案

我想这就是您要找的。

查看此fiddle .

HTML

<header>
    This is the header!
</header>
<article>
    <p>Goodness one unimaginative rooster some that circa much jay goodness gosh pessimistic scantly dark some modest gasped where unexplainable some before about that a hello firefly macaw ethereally indecisive panda lorikeet where a peskily jellyfish a since lied factiously in and that after but falteringly so worm flabbily yet hey gladly more versus much precarious because a the boisterous quiet fed one alas hawk flawlessly thanks some.</p>
    <p>Squirrel much krill regarding before contrary jeez outdid warthog immaturely articulately valiant hey suggestive thus far won darn wow scorpion portentously more hotly miraculous jeepers meagerly since excluding that however pinched yet whale adoringly floppy tenably wow and after via for unproductively as one prior the more on urchin rang much because some more approving near oh a as far lion some lucky far much frog far and beneath underlay far after nosy jaguar subconscious after this far input frequently distant the much positively gagged jeez unobtrusively far sank less before babbled far by when this the this sensible outside.</p>
    <p>That far qualitatively intrepid vulture a ferret disgraceful moaned therefore easy much and curt insincerely dachshund notwithstanding yikes and dragonfly and patted until much jeez close fumed divisive copiously wherever near near far perceptible shrewdly ground yikes opened as elaborate adversely spilled that creepy imaginative by in hello groundhog magnificently jeez hence crud versus desperately caterpillar checked one wicked far some yikes.</p>
    <p>And darn dependent that urchin upon much jeez ably sniffed a less in far darn far incorrect between inept caterpillar man-of-war manta hence ironically into more amenable negative sanctimoniously tortoise eagle far spoiled clapped tepid yikes irresistibly testy warthog hugged immense much immense alas thus paid therefore agitatedly about well following the hey aboard and that after and condescending seagull because alas slept hey this went dove far much far considering raccoon witless under ungraceful.</p>
    <p>Emptied much gosh circuitously inside along a far pointed fanatic hey bravely with far yikes capybara meadowlark sedulous some lion squid floated darn drolly underwrote loving spat maliciously coaxing unwilling goodness seagull elephant snickered balked sloth for kookaburra inside untactfully so the where much human then because pungently cracked heard overtook firm skeptically reverently much some jeez less and hey towards.</p>
</article>
<footer>
    This is the footer!
</footer>

CSS

body {
    font-family: sans-serif;
}

header, footer {
    position: fixed;
    height: 100px;
    text-align: center;
    width: 100%;
    background: #ff0450;
    color: #fff;
    text-transform: uppercase;
    line-height: 6em;
}

header {
    top: 0;
}

footer {
    bottom: 0;
}

article {
    margin: 100px 50px 0;
}

article p {
    margin: 20px 0;
}

article p:first-line {
    font-variant: small-caps;
}

更新

body {
    font-family: sans-serif;
}

header, footer {
    position: fixed;
    height: 100px;
    text-align: center;
    width: 100%;
    background: #ff0450;
    color: #fff;
    text-transform: uppercase;
    line-height: 6em;
}

header:before {
    content: "";
    position: absolute;
    height: 20px;
    background: #fff;
    z-index: 999;
    top: -20px;
    width: 100%;
    left: 0;
}

footer:before {
    content: "";
    position: absolute;
    height: 20px;
    background: #fff;
    z-index: 999;
    bottom: -20px;
    width: 100%;
    left: 0;
}

header {
    top: 0;
    margin-top: 20px;
}

footer {
    bottom: 0;
    margin-bottom: 20px;
}

article {
    margin: 120px 50px 0;
}

article p {
    margin: 20px 0;
}

article p:first-line {
    font-variant: small-caps;
}

更新了fiddle也是。

关于HTML -> 固定顶部、固定底部和动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116964/

相关文章:

html - 背景图片不适合移动设备 - CSS

html - 如何使这种样式成为外部 CSS/SCSS 文件?

android - Android SDK 1.5和2.0在渲染方面有什么区别?

css - 在 Angular 中,如何在 scss 文件中使用组件变量

css - 将导航栏和页脚中的内容居中

python - 框架在 Tkinter 中是否有单独的框架位置?

css - 固定div的位置

javascript - 批量创建标签二维码

android - Android 上使用 Phonegap 3.4 的 HTML5 视频

jquery - 如何使用 jQuery 更改多个元素的字体大小