始终让footer在底部
2024-09-01 18:28:00
1.footer保持在页面底部
需求:
我们希望footer能在窗口最底端,但是由于页面内容太少,无法将内容区域撑开,从而在 footer 下面会留下一大块空白
第一种方法:采用 flexbox布局模型
(将 body 的 display 属性设置为 flex, 然后将方向属性设置为列, (默认是行,也就是横向布局);同时,将html 和 body 元素的高度设置为100%,使其充满整个屏幕)
代码:
<div id="container">
<header>header</header>
<section class="main">main</section>
<footer>footer</footer>
</div>
*{
margin:;
padding:;
}
html,body{
height: 100%;
}
#container{
display: flex;
flex-direction: column;
height: 100%;
}
header{
background: #999;
flex: 0 0 auto;
}
.main{
background: orange;
flex: 1 0 auto;
}
footer{
background: #333;
flex: 0 0 auto;
}
第二种方法:采用footer高度固定+绝对定位
(注意:footer的父层的最小高度是100%,footer设置成相对于父层位置绝对(absolute)置底(bottom:0),父层内要预留(padding-bottom)footer的高度,保证main的内容能够全部显示出来而不被footer遮盖)
代码:
<div id="container">
<header>header</header>
<section class="main">main</section >
<footer>footer</footer>
</div>
*{
margin:;
padding:;
}
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;
}
第三种:固定在网页底部且居中
html {
height: 100%;
}
body {
margin:;
padding:;
min-height: 100%;
position: relative;
}
#footer{
position: absolute;
left:;
right:;
bottom:;
color: #969696;
text-align: center;
}
附加大佬的常用居中总结:
https://juejin.im/post/58f818bbb123db006233ab2a#heading-6
最新文章
- WordPress目录文件结构详细说明
- DP专题训练之HDU 2955 Robberies
- 【iCore3 双核心板_FPGA】实验二十一:Niosii——基于内部RAM建立第一个软核
- AWR报告-数据库概要信息(一)
- make clean-kernel &;&; make kernel
- .net 中连接mysql
- 手写java虚拟机(一)——搭建环境
- JavaScript面试的完美指南(开发者视角)
- 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)
- [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
- HTML- 标签语法
- python+selenium基础之XPATH轴定位(第二篇)
- shell 脚本不能执行多条?何解
- 【OCR技术系列之八】端到端不定长文本识别CRNN代码实现
- Apache Solr入门教程(初学者之旅)
- Lodop打印条码二维码设置多宽不一定是多宽
- 在k8s集群中,利用prometheus的jmx_exporter进行tomcat的JVM性能监控,并用grafana作前端展示
- 复习:JSP基本的语法(JSP凝视 + JSP指令 + JSP脚本元素 + JSP动作元素)
- Extjs4.x treepanel,treegrid 节点选择,选中某个节点
- POJ 3308 Paratroopers 最大流,乘积化和 难度:2