search
HomeWeb Front-endHTML Tutorial固定网页页脚的最佳方法(译)_html/css_WEB-ITnose

原文地址:http://tutorialzine.com/2016/03/quick-tip-the-best-way-to-make-sticky-footers/

在开发网页布局的时候,你可能已经遇到了这个问题:

你可能会设置一个页脚在 body 的最后一部分,但是当页面中没有太多内容的时候,页脚就会留在屏幕的中间,它的下面会出现很大面积的空白。

在这个小教程中,我们将使用现代化的技术来构建一个页脚,保证页脚在任何时候都固定在页面的底部。

为了防止上述情况的发生,我们将使用 Flexbox 来建立我们的网页,它是 CSS3 所提供的建立响应式布局的方法。对于那些你不熟悉的 Flexbox 的模型和它的属性,我会留下一些链接在文章的结尾。

我们使用的例子很简单,头部、主要部分、页脚。下面是 HTML,没有什么特别的部分。

<body>    <header>...</header>    <sectionclass="main-content">...</section>    <footer>...</footer></body> 

为了使用 Flex,我们将 body 的 display 属性设置为 flex,然后将 flex-direction 设置为 column(默认是 horizontal),另外设置 html 和 body 的高度为 100%,填充整个屏幕。

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

现在我们将设置下面的这几个 flex 属性,网页中每个部分占用的空间。

  • flex-grow:元素在一个容器中对可分配空间占用的比率。
  • flex-shrink:如果空间不足,元素收缩的比率。
  • flex-basis:元素的默认值。

我们希望页眉和页脚占用固定的一部分空间,剩下的空间全都分成主要内容。这样的布局 CSS 如下:

header{  /* We want the header to have a static height,   it will always take up just as much space as it needs.  */  /* 0 flex-grow, 0 flex-shrink, auto flex-basis */  flex: 0 0 auto;} .main-content{  /* By setting flex-grow to 1, the main content will take up   all of the remaining space on the page.   The other elements have flex-grow: 0 and won't contest the free space. */  /* 1 flex-grow, 0 flex-shrink, auto flex-basis */  flex: 1 0 auto;} footer{  /* Like the header, the footer will have a static height - it shouldn't grow or shrink.  */  /* 0 flex-grow, 0 flex-shrink, auto flex-basis */  flex: 0 0 auto;} 

你可以点击下面的图片查看我们的演示的页面,点击粉色的达按钮,你可以改变网页的内容量,你将观察到无论什么时候页脚都是在网页的底部。

结论

正如你所看到的,Flexbox 在布局中是一个强有力的工具。几乎所有的主流浏览器都支持它,IE 浏览器需要在版本在 IE9+。

这里有一些学习 Flexbox 的经验:

  • CSS 技巧之 Flexbox。 here
  • 专门使用 Flexbox 技术的网站。 here
  • 5分钟的互动课程。 here
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 as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

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.

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

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software