How to create a sticky footer that stays at the bottom of the page in CSS

Arif Billah Babu

         

  CSS



Image not found!!


Creating a sticky footer that stays at the bottom of the page in CSS can be achieved using a combination of Flexbox or Grid layout and CSS positioning. Here's a basic example using Flexbox:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sticky Footer</title> <style> html, body { height: 100%; margin: 0; } body { display: flex; flex-direction: column; } .content { flex: 1; /* Add your main content styles here */ } .footer { background-color: #333; color: #fff; padding: 10px; text-align: center; /* Sticky footer styles */ position: sticky; bottom: 0; } </style> </head> <body> <div class="content"> <!-- Your main content goes here --> <p>This is your main content.</p> </div> <div class="footer"> <!-- Footer content goes here --> <p>This is the sticky footer.</p> </div> </body> </html>

In this example:

  1. The html and body elements are set to a height of 100% to ensure that the container takes up the full height of the viewport.
  2. The body is set to a flex container with a column direction, allowing the content to flexibly take up available space.
  3. The .content div has flex: 1, which means it will take up the available space and push the footer to the bottom.
  4. The .footer div is styled with a background color, padding, and positioned as sticky with bottom: 0, ensuring it sticks to the bottom of the viewport when the content is not tall enough.

Adjust the styles and content to fit your specific needs.