宽度自适应的问题

单页面应用里,想让footer固定在底部,用的postion,然而如果文章内容超出一屏,footer就会定位在一屏的地方。
如果不写html 100%的宽度,其他不足一屏的页面定位就不在页面底部了,有什么解决的方案吗?

阅读 3.9k
4 个回答

已解决,需要根据html定位

用position:fixed

<!DOCTYPE html>
<html>
<head>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        html, body {
            height: 100%;
        }
        div {
            position: relative;
            min-height: 100%;
        }
        footer {
            position: absolute;
            bottom: 0px;
            background: grey;
            width: 100%;
            height: 50px;
        }
        p {
            height: 2000px;
        }
    </style>
</head>
<body>
<div>
    <p></p>
    <footer>123</footer>
</div>
</body>
</html>

是要这样?

一般来说如果要用到百分比布局必须为html和body加上宽高100%
你知道嘛?有一种布局方案是用flex来布局

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        html,body {
            width: 100%;
            height: 100%;
        }
        .page {
            width: 100%;
            height: 100%;
            display: flex;
            /*使得内部元素竖直排列*/
            flex-direction: column;
        }
        .content {
            /*将剩余空间全部填满*/
            flex: 1;
            background: #f00;
            width: 100%;
        }
        .footer {
            height: 100px;
            width: 100%;
            background: #ff0;
        }
    </style>
</head>
<body>
    <div class="page">
        <div class="content"></div>
        <div class="footer"></div>
    </div>
</body>
</html>

效果:

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题