html 图片沉底代码,网站footer沉底效果的三种解决方案

问题背景

很多网站设计一般是两个部分,content + footer,content里面装的是网站主体内容,footer里面展示网站的注册信息等等,因为网站内容高度不定的原因,会出现下面两种情况:

1.内容较少时,这个footer固定在在页面的底部。如下所示:

bVbtamr?w=506&h=477

2.内容较长时,footer跟在内容后面滑动,大致表现如下图红色框起来的部分(对应网址:http://www.sbc-mcc.com/):

bVbtagx?w=1917&h=970

这个需求在PC端还是很常见的,我在自己的应用中也遇到了这个问题,今天总结了一下实现这种布局的几个方法。

方法1 使用js计算

为什么第一个就采用js控制的呢,因为实不相瞒,当初我第一次遇到这个问题的时候,直接就使用js去解决的(主要是我知道js肯定能实现的,所以也就没有花时间去想别的方法)

主要思路是:在页面加载完成后计算屏幕高度 - content内容真实的高度的值,如果差值大于

footer的高度,就给footer的style加上fixed定位,使它固定在屏幕底部。

demo代码如下:

footer沉底效果

div {

margin: 0,

padding: 0;

box-sizing: border-box;

position: relative;

}

html, body {

width: 100%;

height: 100%;

}

#container {

width: 100%;

height: 100%;

}

#content {

background: blue;

}

#footer {

width: 100%;

height: 100px;

background: red;

}

.footer-fixed {

position: fixed;

left: 0;

bottom: 0;

}

content

let height = document.getElementById('container').clientHeight - document.getElementById('content').clientHeight;

// 这里给footer加上另外的class,使其固定

if (height > 100) document.getElementById('footer').classList.add('footer-fixed');

本着能使用css解决就绝对不使用js的原则,这个方法虽然最容易想到,但是还是不推荐使用,而且,这段css代码要获取clientHeight,将会导致页面页面重排和重绘,性能考虑上来说,也不推荐。

方法2 采用flex布局 + min-height

flex布局中的justify-content: space-between;搭配超级好用的min-height,刚好可以满足在content内容不足的时候,footer的沉底效果

demo代码如下:

footer沉底效果

div {

margin: 0;

padding: 0;

box-sizing: border-box;

position: relative;

}

html, body {

width: 100%;

height: 100%;

margin: 0;

padding: 0;

}

#container {

width: 100%;

// 重点代码

// 虽然不知道container的高度,但是可以设置一个最小高度,这样有利于布局

min-height: 100%;

display: flex;

flex-direction: column;

justify-content: space-between;

}

#content {

background: blue;

}

#footer {

width: 100%;

height: 100px;

background: red;

}

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

footer

min-height实在是超级好用的一个css属性了,搭配flex轻松实现沉底效果。

方法3 巧用flex + margin-top

这个技巧是在讲margin auto的妙用中学到的,在flex格式化上下文中,margin auto会自动去分配剩余空间。这里面我们可以在footer上使用margin-top:auto来达到沉底效果。

footer沉底效果

div {

margin: 0;

padding: 0;

box-sizing: border-box;

position: relative;

}

html, body {

width: 100%;

height: 100%;

margin: 0;

padding: 0;

}

#container {

width: 100%;

min-height: 100%;

display: flex;

flex-direction: column;

}

#content {

background: blue;

}

#footer {

width: 100%;

height: 100px;

background: red;

margin-top: auto; // 重点代码

}

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

content

footer

总结:以上三种方法都属于没什么副作用的,其实实现这种沉底效果还有别的实现方式,但是对其他布局有影响,这里不赘述,之后有了更好的解决方案,再来更新。

PS:之前margin auto没有深入了解过,了解之后发现还是很神奇的,推荐右边文章探秘 flex 上下文中神奇的自动 margin

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值