页面内容不够高footer始终位于页面的最底部

相信很多前端工程师在开发页面时会遇到这个情况:当整个页面高度不足以占满显示屏一屏,页脚不是在页面最底部,用户视觉上会有点不好看,想让页脚始终在页面最底部,我们可能会想到用:

1.min-height来控制content中间内容区高度来让页面高度能够占满显示屏一屏,但是大型网站页面比较多的情况下footer都是模块化添加进去的,每个页面高度都不会一样,不可能去设置每个页面中间内容区min-height高度,而且用户的显示屏的大小都不一样,无法精确设置min-height高度,无法保证用户看到页面页脚不是在最底部或页面不出现滚动条;

2.页脚固定定位:页脚相对于body固定定位会显示在最底部,但是页面有滚动条时,页面滚动,页脚会悬浮在内容区上,可能以上都不是你想要的效果。

可以用下实例方法解决你的问题:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>猿来是勇者</title>
    </head>
   <!--方法一-->
    <style>
        *{margin: 0; padding: 0;}
        html,body{height:100%;}
        #container{position:relative; width:100%; min-height:100%;padding-bottom: 100px; box-sizing: border-box;}
        header{width: 100%; height: 200px; background: #999;}
        .main{width: 100%; height: 200px; background: orange;float:left;}
        footer{width: 100%; height:100px; /* footer的高度一定要是固定值*/ position:absolute; bottom:0px; left:0px; background: #333;}
    </style>
    <body>
        <div id="container">
            <header>HEADER</header>
            <section class="main">MAIN</section >
            <footer>FOOTER</footer>
        </div>
    </body>
    
    <!--方法二-->
    <!--<style>
        *{margin: 0; padding: 0;}
        html,body{height: 100%;}
        #container{display: flex; flex-direction: column; height: 100%;}
        header{background: #999; flex: 0 0 auto;height:100px;}
        .main{flex: 1 0 auto;}
        .bg{background:orange;height:200px;}
        footer{background: #333; flex: 0 0 auto;height:100px;}
    </style>
    <body>
        <div id="container">
            <header>HEADER</header>
            <section class="main">
                <div class="bg">MAIN</div>
            </section>
            <footer>FOOTER</footer>
        </div>
    </body>-->
</html>

 

 

转载于:https://www.cnblogs.com/webdom/p/8727988.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值