原文地址: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

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

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

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.

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

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.

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.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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