Detailed explanation of eight methods to achieve CSS page bottom fixed

Detailed explanation of eight methods to achieve CSS page bottom fixed

When we are writing a page, we often encounter a situation where the page content is small, and the footer will be stuck in the middle of the page or something? Anyway, it is not displayed at the bottom, and it is ugly. The layout to be discussed below is to solve how to make the element stick to the bottom of the browser.

Method 1: Fixed footer height + absolute positioning

html

<div class="dui-container">
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
</div>

CSS

.dui-container{
position: relative;
min-height: 100%;
}
main {
padding-bottom: 100px;
}
header, footer{
line-height: 100px;
height: 100px;
}
footer{
width: 100%;
position: absolute;
bottom: 0
}

View the effect

Method 2: Add a negative value to the bottom margin of the main content equal to the bottom height

html

<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS

html, body {
height: 100%;
}
main {
min-height: 100%;
padding-top: 100px;
padding-bottom: 100px;
margin-top: -100px;
margin-bottom: -100px;
}
header, footer{
line-height: 100px;
height: 100px;
}

View the effect

Method 3: Set the margin-top of the footer to a negative number

html

<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS

main {
min-height: 100%;
padding-top: 100px;
padding-bottom: 100px;
}
header, footer{
line-height: 100px;
height: 100px;
}
header{
margin-bottom: -100px;
}
footer{
margin-top: -100px;
}

View the effect

Method 4: Set the margin-top of footer to auto by setting flex

html

<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS

body{
display: flex;
min-height: 100vh;
flex-direction: column;
}
header,footer{
line-height: 100px;
height: 100px;
}
footer{
margin-top: auto;
}

View the effect

Method 5: Calculate the height of the content using the calc() function

HTML code

<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS Code

main{
min-height: calc(100vh - 200px); /* This 200px is the height of the header and footer*/
}
header,footer{
height: 100px;
line-height: 100px;
}

View the effect

Method 6: Set the main body to flex by setting flexbox

html

<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS Code

body{
display: flex;
min-height: 100vh;
flex-direction: column;
}
main{
flex: 1
}

View the effect

Method 7: Using grid layout

Html code

<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS Code

html {
height: 100%;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: auto 1fr auto;
}
.footer {
grid-row-start: 3;
grid-row-end: 4;
}

View the effect

Method 8: display-*

html

<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS

body {
  min-height: 100%;
  display: table;
  width: 100%;
}
main {
  display: table-row;
  height: 100%;
}

View the effect

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Use of MySQL truncate table statement

>>:  HTML web page creation tutorial Use iframe tags carefully

Recommend

Windows Service 2016 Datacenter\Stand\Embedded Activation Method (2021)

Run cmd with administrator privileges slmgr /ipk ...

11 ways to remove duplicates from js arrays

In actual work or interviews, we often encounter ...

Learn Hyperlink A Tag

ask: I have styled the hyperlink using CSS, but i...

Example analysis of mysql user rights management

This article describes the MySQL user rights mana...

Detailed explanation of linux nslookup command usage

[Who is nslookup?] 】 The nslookup command is a ve...

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation 1....

MySQL series: redo log, undo log and binlog detailed explanation

Implementation of transactions The redo log ensur...

How to modify the firewall on a Linux server to allow remote access to the port

1. Problem Description For security reasons, the ...

Summary of the use of Vue computed properties and listeners

1. Computed properties and listeners 1.1 Computed...

Some basic instructions of docker

Table of contents Some basic instructions 1. Chec...

Useful codes for web page creation

<br />How can I remove the scroll bar on the...

js precise calculation

var numA = 0.1; var numB = 0.2; alert( numA + num...

Summary of common problems and application skills in MySQL

Preface In the daily development or maintenance o...

Specific use of MySQL internal temporary tables

Table of contents UNION Table initialization Exec...