首页 > 其他分享 >【CSS/HTML】footer固定在页面底部的实现方法总结

【CSS/HTML】footer固定在页面底部的实现方法总结

时间:2024-09-30 22:18:39浏览次数:3  
标签:bottom text 100% footer height padding HTML CSS

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

HTML代码:

<body>
    <header>头部</header>
    <main>中间内容</main>
    <footer>底部信息</footer>
</body>

CSS代码:

*{
    margin:0;
    padding:0;
}
html{
    height:100%;
}
body{
    min-height:100%;
    margin:0;
    padding:0;
    position:relative;
}

header{
    background: #000;
    text-align: center;
    height:50px;
    line-height: 50px;
    color:#fff;
}
main{ /* main的padding-bottom值要等于或大于footer的height值 */
    padding-bottom:100px;
    background:#ccc;
    text-align: center;
}
footer{
    position:absolute;
    color:#fff;
    bottom:0;
    width:100%;
    height:100px;
    line-height:100px;
    text-align:center;
    background-color: #000;
}

实现的效果:
在这里插入图片描述

  • 首先,设置body的高度至少充满整个屏幕,并且body作为footer绝对定位的参考节点;
  • 其次,设置main(footer前一个兄弟元素)的padding-bottom值大于等于footer的height值,以保证main的内容能够全部显示出来而不被footer遮盖;
  • 最后,设置footer绝对定位,并设置height为固定高度值

优点:footer一直存在于底部。
缺点:中间区域main如果内容不够,不能撑满页面的中间区域。

方法二:footer高度固定+margin负值

HTML代码:

<body>
    <div class="container">
        <header>头部</header>
        <main>中间内容</main>
    </div>
    <footer>底部信息</footer>
</body>

CSS代码:

*{
    margin:0;
    padding:0;
}
html,body{
    height:100%;
}
.container{
    min-height:100%;
}

header{
    background: #000;
    text-align: center;
    height:50px;
    line-height: 50px;
    color:#fff;
}
main{ 
    padding-bottom:100px;
    background:#ccc;
    text-align: center;
}
footer{
    color:#fff;
    height:100px;
    line-height:100px;
    margin-top:-100px;
    text-align:center;
    background-color: #000;
}

此方法把footer之前的元素放在一个容器里面,形成了container和footer并列的结构:
首先,设置.container的高度至少充满整个屏幕;
其次,设置main(.container最后一个子元素)的padding-bottom值大于等于footer的height值;
最后,设置footer的height值和margin-top负值

展示效果跟第一种是一样的,缺点跟第一种也是一样的。

方法三:footer高度任意+js

HTML代码:

<header>头部</header>
<main>中间内容</main>
<footer>底部信息</footer>

CSS代码:

*{
    margin:0;
    padding:0;
}
html{
    height:100%;
}
body{
    min-height:100%;
    margin:0;
    padding:0;
    position:relative;
}

header{
    background: #000;
    text-align: center;
    height:50px;
    line-height: 50px;
    color:#fff;
}
main{ /* main的padding-bottom值要等于或大于footer的height值 */
    background:#ccc;
    text-align: center;
}
footer{
    color:#fff;
    width:100%;
    height:100px;
    line-height:100px;
    text-align:center;
    background-color: #000;
}
/* 动态为footer添加类fixed-bottom */
.fixed-bottom {
    position: fixed;
    bottom: 0;
    width:100%;
}

JS(jquery)代码:

$(function(){
    function footerPosition(){
        $("footer").removeClass("fixed-bottom");
        var contentHeight = document.body.scrollHeight,//网页正文全文高度
            winHeight = window.innerHeight;//可视窗口高度,不包括浏览器顶部工具栏
        if(!(contentHeight > winHeight)){
            //当网页正文高度小于可视窗口高度时,为footer添加类fixed-bottom
            $("footer").addClass("fixed-bottom");
        }
    }
    footerPosition();
    $(window).resize(footerPosition);
});

常用的纯css实现footer sticker

HTML代码:

<div id="sticker">
    <div id="stickerCon"></div>
</div>
<div id="footer">footer</div>

CSS代码:

html, body, #sticker {height: 100%;}
    body > #sticker {height: auto; min-height: 100%;}
    #stickerCon {padding-bottom: 40px;} 
    #footer {margin-top:-40px; height: 40px; width: 100%; text-align: center; line-height: 40px; color: #ABA498; font-size: 12px; background: #fafafa; border-top:1px solid #E7E7E7;}
 

标签:bottom,text,100%,footer,height,padding,HTML,CSS
From: https://blog.csdn.net/mengran_code/article/details/142664641

相关文章

  • 【HTML源码】TAB内容显示切换
    分享一段Tab切换源码(HTML)每个选项都对应着不同的内容显示区域,默认显示全部(第一个)页面HTML  <divclass="tabs">    <buttonclass="tab-linkactive"data-tab-target="#all">全部</button>    <buttonclass="tab-link"data-t......
  • 第四章 CSS样式基础
    4.1CSS概述随着HTML的发展,为了满足页面设计者的要求,HTML添加了很多显示功能,但是随着这些功能的增加,使得HTML变得越来越杂乱,HTML页面也越来越臃肿,CSS便诞生了。CSS是用于简化HTML标签,把关于样式部分的内容提取出来,进行单独地控制,使结构与样式分离式开发。对页面布局等的控制......
  • html2canvas图片跨域问题
    需求:页面有个弹窗,弹窗内部有网站logo、表格、第三方的图片等内容,点击打印按钮,将弹窗区域内容下载至本地安装依赖pnpmaddhtml2canvas引入importhtml2canvasfrom'html2canvas'使用<template>...<button@click="handlePrint()">打印</button></template><s......
  • html5添加视频
    <videosrc="video/puppy.mp4" poster="images/puppy.jpg" width="400"height="300" preload controls loop> <p>Avideoofapuppyplayinginthesnow</p> </video> </bo......
  • WEB 编程:富文本编辑器 Quill 配合 Pico.css 样式被影响的问题之还是 iframe
    这个系列已经写了3篇了。这篇写如何使用iframe解决标题里面提到的问题。前情提要请看上一篇博文:WEB编程:富文本编辑器Quill配合Pico.css样式被影响的问题之ShadowDOMWEB编程:富文本编辑器Quill配合Pico.css样式被影响的问题之ShadowDOM-CSDN博客缘由缘由仍......
  • 基于Python+flask+MySQL+HTML的全国范围水质分析预测系统,可视化用echarts,预测算法随机
    1绪论近年来,水质监测系统的进步显著,这在全球环保意识不断提升的背景下尤为明显。大量资源被投入到水质监测技术的研发和应用中,以不断优化监测效能。水资源的保护及健康环境的维护,这种趋势旨在提升人们生活质量,确保优质的生活条件。通过持续不懈的努力,我们得以实现对水质的及......
  • css基础2
    一:页面布局盒模型边界(border)和其他内容的距离是(margin)文本内容和边界的距离是(padding)padding和margin属性单位em是当前文本的大小,2em是两倍文本的距离四个值按顺时针方向代表top,right,bottom,lefth1{padding:1em2em3em4em;}h1{margin:1em2em3em4em;}两个值依次代表t......
  • 2、HTML 常用文本格式化标签
    学习是一场马拉松,不在于瞬间的爆发,而在于途中的坚持。文章目录HTML常用文本格式化标签前言1.标签汇总2.示例代码3.运行截图HTML常用文本格式化标签前言HTML中的文本格式化标签,可以为文字设置租体、斜体或下划线等效果,使文字以特殊的方式显示,以此和普通文......
  • Html三级menu
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title></head......
  • html5一些特性
    1.添加Html5标识头<!DOCTYPEhtml>2.注释<!---->3.块元素<h1><p><ul><li><div>4.内联元素<a><b><em><img><span>5.内联框架iframe<body> <iframewidth="450"height="350"......