页面内容不够高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; 100%; min-height:100%;padding-bottom: 100px; box-sizing: border-box;}
        header{ 100%; height: 200px; background: #999;}
        .main{ 100%; height: 200px; background: orange;float:left;}
        footer{ 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