美文网首页
关于页面的头尾固定中间高度自适应的小结

关于页面的头尾固定中间高度自适应的小结

作者: 遇见一只咩 | 来源:发表于2017-09-13 10:55 被阅读0次
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
    <style>
   /* body{
        margin:0;
    }
        .warper{
            height:100%;
            display: flex;
            flex-direction:column;
            align-content:stretch;
        }
        .header{
            height:5rem;
            background-color: red;
            order: 0;
            flex-shrink: 0;
        }
        .footer{
            height:3rem;
            background-color: rgba(0,255,255,0.5);
            order: 2;
            flex-shrink: 0;
        }
        .container{
            flex-grow:999;
            order: 1;
            overflow:hidden;
            position: relative;
        }
        .container .baseline{
            min-height:100%;
            height:100%;
            overflow:auto;
        }
        .baseline > p:nth-of-type(odd){
            background-color: pink
        }
        .baseline > p:nth-of-type(even){
            background-color: lightgreen
        }*/

        *{
            margin:0;
            padding: 0;
        }
        .container{
            
            overflow:hidden;
           
        }
        .warper{
            height:100%;
            padding-top: 5rem;
            padding-bottom: 3rem;
            position: relative;
        }
        .header{
            text-align: center;
            height:5rem;
            width: 100%;
            background-color: red;
            position: fixed;
            top: 0;
            
        }
        .footer{
            text-align: center;
            height:3rem;
            width: 100%;
            background-color: rgba(0,255,255,0.5);
            position: fixed;
            bottom: 0;
        }
        .item{
            height: 2rem;
            border-bottom: .1rem solid;
        }
        .container .baseline{
            min-height:100%;
            height:100%;
            overflow:auto;
        }
        .baseline > p:nth-of-type(odd){
            background-color: pink
        }
        .baseline > p:nth-of-type(even){
            background-color: lightgreen
        }
    </style>
</head>
<body>
    <div class="warper">
        <div class="header">header</div>
        <section class="container">
            <div class="baseline">
                <p class="item">item1</p>
                <p class="item">item2</p>
                <p class="item">item3</p>
                <p class="item">item4</p>
                <p class="item">item5</p>
                <p class="item">item6</p>
                <p class="item">item7</p>
                <p class="item">item8</p>
                <p class="item">item9</p>
                <p class="item">item10</p>
                <p class="item">item11</p>
                <p class="item">item12</p>
                <p class="item">item13</p>
                <p class="item">item14</p>
                <p class="item">item15</p>
                <p class="item">item16</p>
                <p class="item">item17</p>
                <p class="item">item18</p>
                <p class="item">item19</p>
                <p class="item">item20</p>
            </div>
        </section>
        <div class="footer">footer</div>
    </div>
</body>
</html>

实现的效果:
页面分为header、container、footer三个部分;header、footer固定,中间部分自适应高度;并且这个效果对于不同手机能够适配。
关于这个效果的小结:
1.利用display:flex。给父级元素(warper)设置display:flex;调整主轴方向为column。设置header的高度,order:0,flex-shrink:0。footer的高度,order:2,flex-shrink:0。container的设置是:flex-grow:999(这样中间部分就可以自动拉伸和缩减);order:1(确定container的顺序是在中间)。
2.百分比布局。关于百分比布局的参照或者说基准是什么?之前我一直认为是父级元素,但是这中看法并不完整,当父级元素有设置高度时,参照是父级,否则的话就向上一级去寻找设置高度的元素,作为参照。在本文的例子中baseline的参照是warper(是不是很震惊),忽然不是container。
3.拓展一下:我之前的做法是在warper设置padding-top和padding-bottom分别给header和footer让出高度。

相关文章

  • 关于页面的头尾固定中间高度自适应的小结

    实现的效果:页面分为header、container、footer三个部分;header、footer固定,中间部...

  • 记录H5 C3的一些奇淫技巧

    关于flex布局 项目经常遇到 上下固定中间自适应剩余高度的场景 如果中间的盒子用了 flex:1; 会遇到 中...

  • css圆角框学习

    宽度固定高度自适应 高度宽度自适应

  • 常见网页布局的介绍

    左侧固定,右侧自适应 右侧固定,左侧自适应 圣杯布局(左右固定,中间自适应) 中间固定 两侧自适应 等分布局 等分...

  • 垂直滚动

    需求: 顶部固定高度 底部固定高度 中间高度自适应,并且可以出发内部滚动而不出现横向滚动和垂直动条 解决方式一:中...

  • 2019-04-01常见布局汇总(flex版本)

    1,左侧固定中间自适应 2,右侧固定,左侧自适应 3,两边固定中间自适应 4,等高布局

  • css布局

    简单的布局问题 高度已知,三栏布局,要求左右宽度固定,中间自适应,怎么实现?

  • 02-布局中margin和padding区别

    需求:1)上下两个盒子固定定位,中间的盒子自适应高度。撑大剩余盒子高度。 注意:1)中间的盒子只能使用paddin...

  • table 头尾固定,中间滚动

    上网试过了很多方法,我总结了一下是以下两种:法一:用 table 内部属性 thead tbody tfoot...

  • 页面布局

    问题:实现两边200px固定,中间自适应的布局,高度固定为400px 1.实现一 :float布局 浮动,绝对定位...

网友评论

      本文标题:关于页面的头尾固定中间高度自适应的小结

      本文链接:https://www.haomeiwen.com/subject/bceksxtx.html