android html footer 固定,footer保持在页面底部的两种方法

方法一:footer高度固定+绝对定位

思路:footer的父层的最小高度是100%,footer设置成相对于父层位置绝对(absolute)置底(bottom:0),父层内要预留(padding-bottom)footer的高度。

html代码

HEADER

MAIN

FOOTER

css代码

*{

margin: 0;

padding: 0;

}

html,body{

height: 100%;

}

#container{

/*保证footer是相对于container位置绝对定位*/

position:relative;

width:100%;

min-height:100%;

/*设置padding-bottom值大于等于footer的height值,以保证main的内容能够全部显示出来而不被footer遮盖;*/

padding-bottom: 100px;

box-sizing: border-box;

}

header{

width: 100%;

height: 200px;

background: #999;

}

.main{

width: 100%;

height: 200px;

background: orange;

}

footer{

width: 100%;

height:100px; /* footer的高度一定要是固定值*/

position:absolute;

bottom:0px;

left:0px;

background: #333;

}

方法二:采用 flexbox布局模型

思路:我们将 body 的 display 属性设置为 flex, 然后将方向属性设置为列, (默认是行,也就是横向布局);同时,将html 和 body 元素的高度设置为100%,使其充满整个屏幕。

css代码

*{

margin: 0;

padding: 0;

}

html,body{

height: 100%;

}

#container{

display: flex;

flex-direction: column;

height: 100%;

}

header{

background: #999;

flex: 0 0 60px;

}

.main{

background: orange;

flex: 1 0 auto;

}

footer{

background: #333;

flex: 0 0 60px;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值