方法一:footer高度固定+绝对定位
HTML结构:
<body> <header>header</header> <main>main content</main> <footer>footer</footer></body>
CSS设置:
html{height:100%;}body{min-height:100%;margin:0;padding:0;position:relative;}header{background-color: #ffe4c4;}main{padding-bottom:100px;background-color: #bdb76b;}/* main的padding-bottom值要等于或大于footer的height值 */footer{position:absolute;bottom:0;width:100%;height:100px;background-color: #ffc0cb;}
首先,设置body的高度至少充满整个屏幕,并且body作为footer绝对定位的参考节点;
其次,设置main(footer前一个兄弟元素)的padding-bottom值大于等于footer的height值,以保证main的内容能够全部显示出来而不被footer遮盖;
最后,设置footer绝对定位,并 设置height为固定高度值。
方法二:footer高度固定+margin负值
HTML结构:
<body> <div class="container"> <header>header</header> <main>main content</main> </div> <footer>footer</footer></body>
CSS设置:
html,body{height:100%;margin:0;padding:0;}.container{min-height:100%;}header{background-color: #ffe4c4;}main{padding-bottom:100px;background-color: #bdb76b;}/* main的padding-bottom值要等于或大于footer的height值 */footer{height:100px;margin-top:-100px;background-color: #ffc0cb;}/* margin-top(负值的)高度等于footer的height值 */
此方法把footer之前的元素放在一个容器里面,形成了container和footer并列的结构:
首先,设置.container的高度至少充满整个屏幕;
其次,设置main(.container最后一个子元素)的padding-bottom值大于等于footer的height值;
最后,设置footer的height值和 margin-top负值。
这种方法没有使用绝对定位,但html结构的语义化不如方法一中的结构清晰。
也可以设置负值的margin-bottom在.container上面,此时html结构变化如下:
<body> <div class="container"> <header>header</header> <main>main content</main> <div class="empty"></div> </div> <footer>footer</footer></body>
CSS设置:
html,body{height:100%;margin:0;padding:0;}.container{min-height:100%;margin-bottom:-100px;}.empty,footer{height:100px;}
使用一个空的div把footer容器推到页面最底部,同时给container设置一个负值的margin-bottom,这个margin-bottom与footer和.empty的高度相等。
虽然多了一个空的div,语义上也不怎么好,但是相比前面为main元素设置padding-bottom的方法有一个明显的好处:当网页内容不满一屏的时候,如果需要为main元素设置边框、背景色的时候,padding-bottom硬生生地撑出了一片空白,真真是有点丑,但是加个空的div之后,布局方式作用在.empty上,对main的css设置就不影响了,算是一个好处吧!
方法三:footer高度任意+js
HTML结构:
<body> <header>header</header> <main>main content</main> <footer>footer</footer></body>
CSS设置:
html,body{margin:0;padding: 0;}header{background-color: #ffe4c4;}main{background-color: #bdb76b;}footer{background-color: #ffc0cb;}/* 动态为footer添加类fixed-bottom */.fixed-bottom {position: fixed;bottom: 0;width:100%;}
js代码:
$(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"); } else { $("footer").removeClass("fixed-bottom"); } } footerPosition(); $(window).resize(footerPosition);});

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
