Table of Contents
解决方法
结论
Home Web Front-end HTML Tutorial 告诉你一个将 footer 保持在底部的最好方法_html/css_WEB-ITnose

告诉你一个将 footer 保持在底部的最好方法_html/css_WEB-ITnose

Jun 21, 2016 am 08:50 AM

原文: Quick Tip: The Best Way To Make Sticky Footers

当你在布局网页时,有可能会遇到类似下面的这种情况

导致这一问题的原因是页面内容太少,无法将内容区域撑开,从而在 footer 下面留下一大块空白。

本文将介绍一种现代化的方法,确保 footer 始终处于页面的底部。

解决方法

解决该问题的最好方法是采用 flexbox——CSS3提供的一种先进布局模型,旨在建立具有适应性的布局。如果你对 flexbox 还不怎么熟悉,文章最后有一些扩展阅读链接,可以帮助你了解 flexbox。

我们的演示页面应该具备 Header、主体内容区域和 Footer,下面是该页面的 HTML

<body>    <header>...</header>    <section class="main-content">...</section>    <footer>...</footer></body>
Copy after login

为了启用 flex模式,我们将 body 的 display 属性设置为 flex, 然后将方向属性设置为列, (默认是行,也就是横向布局)。同时,将html 和 body 元素的高度设置为100%,使其充满整个屏幕。

html{    height: 100%;}body{    display: flex;    flex-direction: column;    height: 100%;}
Copy after login

现在,我们需要调整各个区域占用的页面空间,我们将通过flex 属性来达到这一目的,该属性实际包含了三个属性,分别是:

  • flex-grow:元素在同一容器中对可分配空间的分配比率,及扩展比率
  • flex-shrink:如果空间不足,元素的收缩比率
  • flex-basis:元素的伸缩基准值

我们希望 header 和footer 只占用他们应该占用的空间,将剩余的空间全部交给主体内容区域

header{   /* 我们希望 header 采用固定的高度,只占用必须的空间 */   /* 0 flex-grow, 0 flex-shrink, auto flex-basis */   flex: 0 0 auto;}.main-content{   /* 将 flex-grow 设置为1,该元素会占用所有的可使用空间 而其他元素该属性值为0,因此不会得到多余的空间*/   /* 1 flex-grow, 0 flex-shrink, auto flex-basis */   flex: 1 0 auto;}footer{   /* 和 header 一样,footer 也采用固定高度*/   /* 0 flex-grow, 0 flex-shrink, auto flex-basis */   flex: 0 0 auto;}
Copy after login

最终的效果如下图所示。通过点击中央的粉红按钮改变主体内容,footer 会始终显示在页面的最底部。

demo 页面地址: http://demo.tutorialzine.com/2016/03/quick-tip-the-best-way-to-make-sticky-footers/ (看效果可能需要翻墙)

结论

如你说见,如果是从零开始构建布局,flexbox 将会是你的得力助手。除了极少数的例外,所有的主流浏览器都支持 flexbox,就 IE 来说,IE9以后的版本都是支持的。

下面是一些学习 flexbox 布局模型不错的教程和速查表

  • CSS-Trick 的 flex box 快速指南
  • 一个专门提供酷炫 flexbox 技术的网站: Solved by Flexbox
  • 5分钟互动教程
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)

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 <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

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 <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 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

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles