告诉你一个将 footer 保持在底部的最好方法_html/css_WEB-ITnose
原文: 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>
为了启用 flex模式,我们将 body 的 display 属性设置为 flex, 然后将方向属性设置为列, (默认是行,也就是横向布局)。同时,将html 和 body 元素的高度设置为100%,使其充满整个屏幕。
html{ height: 100%;}body{ display: flex; flex-direction: column; height: 100%;}
现在,我们需要调整各个区域占用的页面空间,我们将通过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;}
最终的效果如下图所示。通过点击中央的粉红按钮改变主体内容,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分钟互动教程

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



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

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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

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