search
HomeWeb Front-endHTML Tutorial告诉你一个将 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分钟互动教程
Statement
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
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

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

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

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 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.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor