Home Web Front-end HTML Tutorial DIV+CSS:页脚永远保持在页面底部_html/css_WEB-ITnose

DIV+CSS:页脚永远保持在页面底部_html/css_WEB-ITnose

Jun 21, 2016 am 08:58 AM

页脚永远保持在页面底部

有时候,我们用CSS创建一个高度自适应布局,如何保证页脚(footer)在内容不超过一屏的情况下始终保持在布局最下方是一个比较头疼的事。我看过一些利用绝对定位的例子,但总感觉不是那么完美,经过一下午的研究总结出一个利用负值外补丁的方法来实现这个效果的方法,兼容IE5.0 ,Opera 8.5 ,Firefox 1.5 。下面我们看步骤:

1、为了让浏览器识别高度100%我们需要先给 html 和 body 加上一个高度值,同时清除所有元素的 margin 和 padding。经测试,html和body的 height: 100%; 等于整个浏览器窗口的总高度,无论内容是否超过一屏。而它们下一级子元素 height: 100%; 则等于第一屏的高度。是不是有点不好理解?

css 代码

  1. * {
  2. margin:  0;
  3. padding:  0;
  4. }
  5. html, body {
  6. height:  100%;
  7. }

2、因为上面提到的问题,所以为了让布局自适应高度,我们要加上 min-height: 100%;,虽然IE不支持这个属性但是IE的 height: 100%; 有同样的作用:

css 代码

  1. #wrapper {
  2. min-height:  100%;
  3. }
  4. * html  #wrapper {
  5. height:  100%;
  6. }

这样,一个最简单的最小高度满一屏的自适应布局就做好了。为了便于查看,我加了一些宽度和背景色修饰,如下:

css 代码

  1. * {
  2. margin:  0;
  3. padding:  0;
  4. }
  5. html, body {
  6. height:  100%;
  7. text-align:  center;
  8. font: 12px/1.4 Verdana,  sans-serif;
  9. background:  #f00;
  10. }
  11. #wrapper {
  12. width:  770px;
  13. min-height:  100%;
  14. background:  #ccc;
  15. margin:  auto;
  16. text-align:  left;
  17. }
  18. * html  #wrapper {
  19. height:  100%;
  20. }

下面看完整代码的运行效果:

<!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>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

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.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

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.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

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

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

See all articles