Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created September 24, 2023 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/9181f0c3378878a462c10bd7af9ce514 to your computer and use it in GitHub Desktop.
Save code-boxx/9181f0c3378878a462c10bd7af9ce514 to your computer and use it in GitHub Desktop.
Sticky Footers In HTML CSS
/* (A) STICKY FOOTER */
#page-foot {
position: sticky;
bottom: 0; left: 0; z-index: 9;
}
<!DOCTYPE html>
<html>
<head>
<title>Sticky Footer</title>
<link rel="stylesheet" href="X-cosmetics.css">
<link rel="stylesheet" href="1-sticky.css">
</head>
<body>
<!-- (A) DUMMY CONTENTS -->
<main id="page-body">
<p>START</p>
<p style="height:3000px"></p>
<p>END</p>
</main>
<!-- (B) FOOTER -->
<footer id="page-foot">FOOT</footer>
<nav id="page-nav">NAV</nav>
</body>
</html>
/* (A) FIXED FOOTER */
#page-foot {
position: fixed; width: 100%;
bottom: 0; left: 0; z-index: 9;
}
/* (B) PREVENT BOTTOM CONTENT FROM BEING COVERED */
#page-body { padding-bottom: 50px !important; }
<!DOCTYPE html>
<html>
<head>
<title>Fixed Footer</title>
<link rel="stylesheet" href="X-cosmetics.css">
<link rel="stylesheet" href="2-fixed.css">
</head>
<body>
<!-- (A) DUMMY CONTENTS -->
<main id="page-body">
<p>START</p>
<p style="height:3000px"></p>
<p>END</p>
</main>
<!-- (B) FOOTER -->
<footer id="page-foot">FOOT</footer>
</body>
</html>
/* (A) GRID LAYOUT */
body {
width: 100vw; height: 100vh;
display: grid;
grid-template-rows: 1fr auto;
}
/* (B) ALLOW OVERFLOW */
#page-body { overflow: auto; }
<!DOCTYPE html>
<html>
<head>
<title>Grid Footer</title>
<link rel="stylesheet" href="X-cosmetics.css">
<link rel="stylesheet" href="3-grid.css">
</head>
<body>
<!-- (A) DUMMY CONTENTS -->
<main id="page-body">
<p>START</p>
<p style="height:3000px"></p>
<p>END</p>
</main>
<!-- (B) FOOTER -->
<footer id="page-foot">FOOT</footer>
</body>
</html>
/* (A) FULL PAGE BODY */
body { width: 100vw; height: 100vh; }
/* (B) FOOTER */
#page-foot { height: 50px; }
/* (C) BODY */
#page-body {
height: calc(100vh - 50px);
overflow: auto;
}
<!DOCTYPE html>
<html>
<head>
<title>Calc Footer</title>
<link rel="stylesheet" href="X-cosmetics.css">
<link rel="stylesheet" href="4-calc.css">
</head>
<body>
<!-- (A) DUMMY CONTENTS -->
<main id="page-body">
<p>START</p>
<p style="height:3000px"></p>
<p>END</p>
</main>
<!-- (B) FOOTER -->
<footer id="page-foot">FOOT</footer>
</body>
</html>
/* (X) DOES NOT MATTER */
* {
font-family: arial, sans-serif;
font-size: 24px;
box-sizing: border-box;
}
body { padding: 0; margin: 0; }
#page-body, #page-foot, #page-nav { padding: 10px; }
#page-body { background: #ffcac6; }
#page-foot { background: #beccff; }
#page-nav { background: #f5ffa5; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment