Table of Contents
在 wrapper 上用负的 margin-bottom
在 footer 上用负的 margin-top
通过 cacl() 减少 wrapper 高度
There is flexbox
使用 flexbox
使用 grid
Home Web Front-end HTML Tutorial 【译】粘连 Footer 的 5 种方法 | CSS-Tricks_html/css_WEB-ITnose

【译】粘连 Footer 的 5 种方法 | CSS-Tricks_html/css_WEB-ITnose

Jun 21, 2016 am 08:46 AM

原文: http://www.zcfy.cc/article/491

一个简短的历史,如果你愿意那样说的话。

粘连 footer 的目的是让它“支撑”在浏览器窗口的底部。但不总是在底部,如果有足够的内容将页面撑开,footer 可以被撑到网页下方去。但是,如果页面的内容很短,粘连 footer 仍然会出现在浏览器窗口的底部。

在 wrapper 上用负的 margin-bottom

用一个元素将除了 footer 之外的其他内容包起来。给它设一个负的 margin-bottom,让它正好等于 footer 的高度。这是一个最基本的方法( 例子 )。

例子: 用负 margin 粘连 footer

<body>  <div class="wrapper">      content    <div class="push"></div>  </div>  <footer class="footer"></footer></body>
Copy after login
html, body {  height: 100%;  margin: 0;}.wrapper {  min-height: 100%;  /* Equal to height of footer */  /* But also accounting for potential margin-bottom of last child */  margin-bottom: -50px;}.footer,.push {  height: 50px;}
Copy after login

这个方法需要在内容区域加一个额外的元素( .push 元素),这样确保不会因为负 margin 将 footer 提升上来而覆盖了任何实际内容。 .push 元素也最好不要有自己的 margin-bottom,如果有,那么它也得算在负 margin 中,而这又会使得 push 的 height 和 .wrapper 的 margin-bottom 的数字不一样,看起来也不是很好。

用 这个 技术不需要一个 push 元素,但是相应地,在内容外面需要额外再包一层元素来让它产生对应的 padding-bottom。这也是为了防止负 margin 导致 footer 覆盖任何实际内容。

例子: 用负 margin 粘连 footer 2

<body>  <div class="content">    <div class="content-inside">      content    </div>  </div>  <footer class="footer"></footer></body>
Copy after login
html, body {  height: 100%;  margin: 0;}.content {  min-height: 100%;}.content-inside {  padding: 20px;  padding-bottom: 50px;}.footer {  height: 50px;  margin-top: -50px;}
Copy after login

这个技术和前一个有一个同样的缺点,就是它们都需要添加额外的 HTML 元素。

通过 cacl() 减少 wrapper 高度

有一个不需要添加额外元素的 方法 ,那就是通过 cacl() 调整 wrapper 的高度。这样不需要任何附加的元素,只需要两个元素并排共用 100% 高度。

例子: Sticky Footer with calc();

<body>  <div class="content">    content  </div>  <footer class="footer"></footer></body>
Copy after login
Copy after login
Copy after login
.content {  min-height: calc(100vh - 70px);}.footer {  height: 50px;}
Copy after login

注意我这里用 calc() 扣除了 70px,把 footer 固定为 50px。这是假设内容中的最后一个元素有一个 20px 的 margin-bottom。这个 margin-bottom 和 footer 的高度要加在一起从 viewport 高度中扣除。而且,我们在这里还用了 viewport 单位( vh ——译者注),这是另外一个小技巧,能够避免在让 wrapper 高度为 100% 时还得先把 body 高度设为 100%。

There is flexbox

使用 flexbox

上面三种技术的大问题是它们需要 footer 的高度固定。Web 设计中固定高度通常都不好。内容可能改变。我们需要灵活性。固定高度通常要被亮红灯。 使用 flexbox 来实现粘连 footer 不仅不需要任何额外的元素,还可以支持 footer 可变高度。

例子: 用 Flexbox 粘连 Footer

<body>  <div class="content">    content  </div>  <footer class="footer"></footer></body>
Copy after login
Copy after login
Copy after login
html {  height: 100%;}body {  min-height: 100%;  display: flex;  flex-direction: column;}.content {  flex: 1;}
Copy after login

你甚至可以添加一个 header 到 .content 前面或者其他更多内容到后面。使用 flexbox 的诀窍是:

  • 设置 flex: 1 在你希望自动填充窗口空间的子元素上(在我们的例子里是 .content 元素)。
  • 或者,可以设置 margin-top:auto 来让子元素尽可能远离它前面的元素(或者根据需要选择任意一个方向的 margin)。(上面的 flex:1 也可以用 margin-bottom:auto ,内容垂直居中可以用 margin:auto 0 ,flex 布局很奇妙吧——译者注)

记得我们有一个关于一切 flexbox 相关内容的 完整的指南 。

使用 grid

Grid 布局是一种更新的技术(目前 支持它的浏览器比 flexbox 更少 )。我们也有一个关于它的 完整的指南 。用它实现粘连 footer 也相当容易。

例子: 用 Grid 粘连 footer

<body>  <div class="content">    content  </div>  <footer class="footer"></footer></body>
Copy after login
Copy after login
Copy after login
html {  height: 100%;}body {  min-height: 100%;  display: grid;  grid-template-rows: 1fr auto;}.footer {  grid-row-start: 2;  grid-row-end: 3;}
Copy after login

这个例子只能在 Chrome Canary 或者 Firefox 开发版上工作,并且可能在 Edge 下被降级到旧的 grid 布局版本。

英文原文: https://css-tricks.com/couple-takes-sticky-footer/

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 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, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 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.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 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: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 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.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 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 vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

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.

What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

See all articles