Home Web Front-end CSS Tutorial CSS implementation of Sticky Footer tutorial

CSS implementation of Sticky Footer tutorial

Jan 27, 2018 am 10:10 AM
sticky

This article mainly introduces the relevant information about the sample code for implementing Sticky Footer with CSS. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

The so-called "Sticky Footer" is not a new front-end concept and technology. It refers to a web page effect: if the page content is not long enough, the footer is fixed at the bottom of the browser window; When the content is long enough, the footer is fixed at the very bottom of the page. But if the content of the web page is not long enough, the bottom footer will remain at the bottom of the browser window.

Implementation

Method

1. Change the content part Set the bottom margin to a negative number

This is a more mainstream usage. Set the minimum height of the content part to 100%, and then use the negative bottom margin value of the content part to achieve that when the height is not satisfied, the page The feet remain at the bottom of the window and will be pushed out when the height is exceeded.


<body>
  <p class="wrapper">
      content
    <p class="push"></p>
  </p>
  <footer class="footer"></footer>
</body>html, body {
  height: 100%;
  margin: 0;
}
.wrapper {
  min-height: 100%;
  /* 等于footer的高度 */
  margin-bottom: -50px;
}
.footer,
.push {
  height: 50px;
}
Copy after login

This method requires additional placeholder elements in the container (such as .push)

It should be noted that the margin-bottom value of .wrapper needs to It is consistent with the negative height value of .footer, which is not very friendly.

2. Set the top margin of the footer to a negative number

Since we can use negative margin bottom on the container, can we use negative margin top? sure.

Add a parent element outside the content, and make the bottom padding of the content part equal to the value of the footer height.


<body>
  <p class="content">
    <p class="content-inside">
      content
    </p>
  </p>
  <footer class="footer"></footer>
</body>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

However, this method is the same as the previous one, requiring additional unnecessary html elements.

3. Use flexbox layout

The footer height of the above three methods is fixed. Generally speaking, this is not conducive to web page layout: the content will change. They are both elastic and will break the layout once the content exceeds the fixed height. So use flexbox for the footer, so that its height can become larger, smaller, and more beautiful~ (≧∇≦)


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

You can also add a header above or below Add more elements. You can choose one of the following techniques:

  1. flex: 1 Make the height of the content (such as .content) freely scalable

  2. margin-top : auto

##Remember, we have the "Complete Guide to Flexbox (English)"~

4. absolute

Processing through absolute positioning should be a common solution, as long as the footer is always positioned in the reserved space of the main container.


<p class="wrapper">
    <p class="content"><!-- 页面主体内容区域 --></p>
    <p class="footer"><!-- 需要做到 Sticky Footer 效果的页脚 --></p>
</p>html, body {
    height: 100%;
}
.wrapper {
    position: relative;
    min-height: 100%;
    padding-bottom: 50px;
    box-sizing: border-box;
}
.footer {
    position: absolute;
    bottom: 0;
    height: 50px;
}
Copy after login

This solution requires specifying 100% height of html and body, and the padding-bottom of content needs to be consistent with the height of footer.

5. calc

Calculate (window height - footer height) through the calculation function calc to give the minimum height to the content area. No additional style processing is required, and the amount of code is minimal. ,the easiest.


<p class="wrapper">
    <p class="content"><!-- 页面主体内容区域 --></p>
    <p class="footer"><!-- 需要做到 Sticky Footer 效果的页脚 --></p>
</p>.content {
    min-height: calc(100vh - 50px);
}
.footer {
    height: 50px;
}
Copy after login

This is an ideal implementation if there is no need to consider the compatibility of calc() and vh units. The same problem is that the height value of footer needs to be consistent with the calculated value in content.

6. table

Use the table attribute to make the page appear in the form of a table.


<p class="wrapper">
    <p class="content"><!-- 页面主体内容区域 --></p>
    <p class="footer"><!-- 需要做到 Sticky Footer 效果的页脚 --></p>
</p>html, body {
    height: 100%;
}
.wrapper {
    display: table;
    width: 100%;
    min-height: 100%;
}
.content {
    display: table-row;
    height: 100%;
}
Copy after login

It should be noted that there is a common style restriction when using the table solution. Usually attributes such as margin, padding, and border will not meet expectations. The author does not recommend using this solution. Of course, the problem can also be solved: don't write other styles on the table.

7. Use Grid grid layout

grid is much newer than flexbox, and is better and simpler. We also have "Grid Complete Guide (English)"~


<body>
  <p class="content">
    content
  </p>
  <footer class="footer"></footer>
</body>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
Unfortunately, Grid layout currently only supports Chrome Canary and Firefox Developer Edition versions.

Summary

The author has tried all the above implementation solutions in the project. Each implementation method is actually similar, and it also has its own pros and cons. Some of the solutions have restrictive issues and require a fixed footer height; some of them require adding additional elements or hacking methods. Students can choose the most suitable solution based on the specific needs of the page.

Of course, technology is constantly being updated, and there may be many different and better solutions. But I believe everyone’s ultimate goal is the same, for a better user experience!

Related recommendations:

Detailed explanation of Sticky footer layout of CSS classic layout

What is Sticky footer layout?

Sticky Footer Detailed explanation of two routines at the absolute bottom

The above is the detailed content of CSS implementation of Sticky Footer tutorial. For more information, please follow other related articles on the PHP Chinese website!

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

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

See all articles