DIV+CSS:页脚永远保持在页面底部_html/css_WEB-ITnose
页脚永远保持在页面底部
有时候,我们用CSS创建一个高度自适应布局,如何保证页脚(footer)在内容不超过一屏的情况下始终保持在布局最下方是一个比较头疼的事。我看过一些利用绝对定位的例子,但总感觉不是那么完美,经过一下午的研究总结出一个利用负值外补丁的方法来实现这个效果的方法,兼容IE5.0 ,Opera 8.5 ,Firefox 1.5 。下面我们看步骤:
1、为了让浏览器识别高度100%我们需要先给 html 和 body 加上一个高度值,同时清除所有元素的 margin 和 padding。经测试,html和body的 height: 100%; 等于整个浏览器窗口的总高度,无论内容是否超过一屏。而它们下一级子元素 height: 100%; 则等于第一屏的高度。是不是有点不好理解?
css 代码
- * {
- margin: 0;
- padding: 0;
- }
- html, body {
- height: 100%;
- }
2、因为上面提到的问题,所以为了让布局自适应高度,我们要加上 min-height: 100%;,虽然IE不支持这个属性但是IE的 height: 100%; 有同样的作用:
css 代码
- #wrapper {
- min-height: 100%;
- }
- * html #wrapper {
- height: 100%;
- }
这样,一个最简单的最小高度满一屏的自适应布局就做好了。为了便于查看,我加了一些宽度和背景色修饰,如下:
css 代码
- * {
- margin: 0;
- padding: 0;
- }
- html, body {
- height: 100%;
- text-align: center;
- font: 12px/1.4 Verdana, sans-serif;
- background: #f00;
- }
- #wrapper {
- width: 770px;
- min-height: 100%;
- background: #ccc;
- margin: auto;
- text-align: left;
- }
- * html #wrapper {
- height: 100%;
- }
下面看完整代码的运行效果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <br /><br /><html xmlns="http://www.w3.org/1999/xhtml"> <br /><br /><head> <br /><br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <br /><br /><title>DIV+CSS:页脚永远保持在页面底部 _ 海波吧 _ www.haibor8.cn </title> <br /><br /><style type="text/css"> <br /><br />/*<![CDATA[*/ <br /><br />* { <br /><br /> margin: 0; <br /><br /> padding: 0; <br /><br />} <br /><br />html, body { <br /><br /> height: 100%; <br /><br /> text-align: center; <br /><br /> font: 12px/1.4 Verdana, sans-serif; <br /><br /> background: #F00; <br /><br />} <br /><br />#wrapper { <br /><br /> width: 770px; <br /><br /> min-height: 100%; <br /><br /> background: #ccc; <br /><br /> margin: auto; <br /><br /> text-align: left; <br /><br />} <br /><br />* html #wrapper { <br /><br /> height: 100%; <br /><br />} <br /><br />#header { <br /><br /> background: Green; <br /><br /> height: 40px; <br /><br />} <br /><br />#sidebar { <br /><br /> float: left; <br /><br /> width: 200px; <br /><br /> background: Gray; <br /><br />} <br /><br />#content-box { <br /><br /> float: right; <br /><br /> width: 570px; <br /><br /> background: Olive; <br /><br />} <br /><br />#footer { <br /><br /> height: 50px; <br /><br /> background: Background; <br /><br /> width:770px; <br /><br /> margin: auto; <br /><br />} <br /><br />/*]]>*/ <br /><br /></style> <br /><br /></head> <br /><br /><br /><br /><body> <br /><br /><div id="wrapper"> <br /><br /> <div id="header">此处显示 "header" 的内容</div> <br /><br /> <div id="content-box">此处显示 "content-box" 的内容</div> <br /><br /> <div id="sidebar">此处显示 i"sidebar" 的内容</div> <br /><br /></div> <br /><br /><div id="footer">此处显示 "footer" 的内容</div> <br /><br /></body> <br /><br /></html>

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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 viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

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

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.
