如何使用绝对页眉和页脚实现全高度滚动?

问题描述 投票:0回答:2

我很好奇是否有人可以一起实现 100% 的这些要求,我只能实现 80%(#4 很痛苦):

  1. 标题始终位于页面顶部。
  2. 页脚始终位于页面底部。
  3. main 占据页面剩余的中间位置,无论是否溢出。
  4. 当 main 溢出时,会有一个滚动条占据浏览器的 整个 高度(即页眉、正文和页脚旁边)。可选奖励:当 main 很小时,没有滚动条。
  5. 只有主要体验会过度滚动。页眉和页脚不会出现过度滚动。

到目前为止我所做的最好的没有完整的滚动条高度:

<div className="flex flex-col h-full overflow-hidden">
  <header />
  <main className="flex-1 overflow-auto" /> 
  <footer />
</div>

我正在使用 Tailwind,但也可以随意使用纯 CSS。希望它不会像我一样粉碎你的灵魂。

html css reactjs tailwind-css
2个回答
1
投票

你可以试试这个

html代码

<div class="container">
  <header>Header</header>
  <main>
    <div style="width: 300px;">Main Content</div>
  </main>
  <footer>Footer</footer>
</div>

和CSS

<style>
  html, body, #root {
    height: 100%;
    margin: 0;
  }
  
  .container {
    position: relative;
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 60px;
    background-color: #333;
    color: white;
    text-align: center;
    line-height: 60px;
  }

  main {
    flex: 1;
    overflow-y: auto;
    padding-top: 60px;
    padding-bottom: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  footer {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background-color: #333;
    color: white;
    text-align: center;
    line-height: 60px;
  }
</style>

0
投票

你说基本 css 就可以了,所以在这里我给你另一种方法,做一些小的改变,比如不使用像你的 css 类这样的属性或将

position: absolute
更改为
position: fixed

* {margin: 0; padding: 0; box-sizing: border-box;}
div {position: relative; min-height: 100vh;}
header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: lightblue;
    padding: 15px;
}
footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: lightgray;
    padding: 15px;
}
main {
    background: lightgreen;
    min-height: 100vh;
    padding: 63px 15px;
}
<div>
    <header>Header</header>
    <main>
        <h3>Main</h3>
        <p>What is Lorem Ipsum?</p>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <p>Why do we use it?</p>
        <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
        <p>Where does it come from?</p>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
        <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
        <p>Where can I get some?</p>
        <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
    </main>
    <footer>Footer</footer>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.