Set the margin-bottom of.content to negative
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="Width = device - width, initial - scale = 1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Sticky Footer</title>
<style>
html.body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
}
.content {
min-height: 100%;
margin-bottom: -50px; /* is the inverse of the height of.footer */
}
.content-inside {
background-color: #c2ffa9;
}
.push {
height: 50px; /*.push is an extra placeholder element with the same height as.footer */
}
.footer {
height: 50px;
line-height: 50px; /* To center text vertically */
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<! -- div. Content-inside height can be set arbitrarily, and can be adjusted according to the content height -->
<div class="content-inside">
<! -- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
<! Div. Push is an extra placeholder element with the same height as div. Footer -->
<div class="push"></div>
</div>
<div class="footer">Sticky Footer</div>
</body>
</html>
Copy the code
Set margin-top to negative for.footer
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="Width = device - width, initial - scale = 1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Sticky Footer</title>
<style>
html.body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
}
.content {
min-height: 100%;
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
height: 50px;
line-height: 50px; /* To center text vertically */
background-color: #3ba4f9;
margin-top: -50px; /* is the inverse of the height of.footer */
}
</style>
</head>
<body>
<div class="content">
<! -- div. Content-inside height can be set arbitrarily, and can be adjusted according to the content height -->
<div class="content-inside">
<! -- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky Footer</div>
</body>
</html>
Copy the code
Use calc() to set the height of.content
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="Width = device - width, initial - scale = 1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Sticky Footer</title>
<style>
html.body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
}
.content {
/* If there is a space between.content and.footer, subtract */
min-height: calc(100vh - 50px);
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
height: 50px;
line-height: 50px; /* To center text vertically */
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<div class="content-inside">
<! -- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky Footer</div>
</body>
</html>
Copy the code
Use Flexbox elastic layout
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="Width = device - width, initial - scale = 1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Sticky Footer</title>
<style>
html.body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
/* This scheme does not need to fix the height of.footer */
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<div class="content-inside">
<! -- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky Footer</div>
</body>
</html>
Copy the code
Use Grid Grid layout
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="Width = device - width, initial - scale = 1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Sticky Footer</title>
<style>
html.body {
margin: 0;
padding: 0;
height: 100%;
text-align: center;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: 1fr auto;
}
.content-inside {
background-color: #c2ffa9;
}
.footer {
grid-row-start: 2;
grid-row-end: 3;
background-color: #3ba4f9;
}
</style>
</head>
<body>
<div class="content">
<div class="content-inside">
<! -- content -->
<div>Content</div>
<div>Content</div>
<div>Content</div>
</div>
</div>
<div class="footer">Sticky Footer</div>
</body>
</html>
Copy the code