Move the footer down to the bottom of the page

I'm struggling to create a footer that consistently stays at the bottom of the page, regardless of the amount of content on the page. I've tried following various tutorials but haven't had much success. I'm starting to think I must be doing something wrong.

Can anyone provide some guidance or help me troubleshoot? Thank you

EDIT: Here is my code snippet

HTML code for the page

<html>
    <head>
        <title>
            Test Page Footer
        </title>
        <style type="text/css">
            html, body, .container {
                margin: 0;
                padding: 0;
            }
            body {
                background-color: green;
                color: white;
            }
            .container {
                padding-top: 97px;
            }
        </style>
        <script src="/inc/jquery.js"></script>
        <script src="/inc/sticky-footer.js"></script>
    </head>
    <body>
        <div class="container">

            <? include("./inc/navbar"); ?>

            <div class="content">
                Content of the page
            </div>


        <?php include("./inc/footer"); ?>
        </div>
    </body>
</html>

HTML code for the footer

<html>
    <head>
        <title>
            /inc/footer
        </title>
        <meta name="description" content="Site Footer">
        <style type="text/css">
            .footer {
                width: 100%;
                background-color: orange;
                color: white;
            }
        </style>
        <script src="/inc/jquery.js"></script>
        <script src="/inc/footer.js"></script>
    </head>
    <body>
        <div class="footer">
            &copy; ************.altervista.org 2017
        </div>
    </body>
</html>

JavaScript file

// Window load event used just in case window height is dependant upon images
$(window).bind("load", function() { 

       var footerHeight = 0,
           footerTop = 0,
           $footer = $("#footer");

       positionFooter();

       function positionFooter() {

                footerHeight = $footer.height();
                footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";

               if ( ($(document.body).height()+footerHeight) < $(window).height()) {
                   $footer.css({
                        position: "absolute"
                   }).animate({
                        top: footerTop
                   })
               } else {
                   $footer.css({
                        position: "static"
                   })
               }

       }

       $(window)
               .scroll(positionFooter)
               .resize(positionFooter)

});

Answer №1

To achieve maximum flexibility, consider using

display: flex; flex-direction: column; min-height: 100vh;
on the .container element, and add margin-top: auto to the .footer element so it aligns itself to the end of the main axis of the parent flex container.

Make sure to eliminate unnecessary structural elements from your footer content, such as redundant scripts like jquery that are already present in the page. Include only essential elements for the footer itself.

html, body, .container {
  margin: 0;
  padding: 0;
}
body {
  background-color: green;
  color: white;
}
.container {
  padding-top: 97px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  box-sizing: border-box;
}
.footer {
  background-color: orange;
  color: white;
  margin-top: auto;
}
<div class="container">

  <nav>nav</nav>

  <div class="content">
    content of page
  </div>

  <div class="footer">
    &copy; ************.altervista.org 2017
  </div>
  
</div>

Answer №2

There are numerous methods to achieve this goal. One simple approach is to ensure that the content above the footer maintains a minimum height equivalent to the entire screen minus the height of the footer.

<div id="content" style="min-height: calc(100vh - 50px);"> 
    Insert your content here.
</div>
<div id="footer" style="height: 50px"> 
    Add your footer here.
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Item floating in a list

I'm facing a challenging issue that I need help with. It's a bit complicated to explain, but I'll do my best. There's also a picture included to help illustrate the problem. The problem I have is with an ordered list. When the next row ...

Two objects intersecting in space

When using AngularJS for insert and update operations, I encounter a problem where changes made to user data are reflected in the list of users. Additionally, when adding a new user, the last record's data populates all input fields. Code: User List ...

PHP data insertion using filters and validation

I have created a database and implemented a validation system to ensure that fields are not left empty. If the fields are filled and pass through the sanitization function, I then proceed to save the data in the database. <?php // Define variables as ...

The dataLayer in Google Tag Manager is failing to trigger the necessary event

Product Purchase Button: <button id="btnBuy" onclick="SendData();" JavaScript function to track product details: <script> var dataLayer = []; dataLayer.push( { 'ecommerce': { 'detail': { 'actionField' ...

Take charge of Bootstrap 4 dropdown transformation class while creating a Megamenu

I am currently working on creating a Megamenu using Bootstrap 4's dropdown Component. The issue I'm facing is related to Javascript adding CSS styles with transform, such as transform: translate3d(9px, 5px, 0px); This is causing disruption in m ...

What is the reason behind receiving a CSS warning stating "Expected color but found '0' " when using Phaser's setText() function?

Here's how I created my text object: statusBarHP.text = game.add.text(0, 0, '', { font:"16px Arial", fill:"#ffffff", stroke:"#000000", strokeThickness:2 }); For the object that holds the text: statusBarHP = game.add.sprite ...

What is the most effective approach for annotating TypeScript abstract classes that are dynamically loaded?

I am in the process of developing a library that allows for the integration of external implementations, and I am exploring the optimal approach to defining types for these implementations. Illustration abstract class Creature { public abstract makeN ...

Learn how to rearrange the order of Vue elements

I am working with a specific object structure in my code: lines: [{ order: '1', text: ' blue' },{ order: '2', text: 'green' },{ order: '3', text: 'yellow' }] Currently, thi ...

How does reactjs distinguish between render and return?

I am a beginner in the world of JavaScript. I often come across the terms "return" and "render" in various contexts, but I'm curious about their differences. ...

Tips for successfully transferring data using a dynamic key in a Semantic UI Vue dropdown

My current challenge involves troubleshooting the semantic-ui-vue dropdown functionality. To view the issue, visit my sandbox link: https://codesandbox.io/s/3qknm52pm5. Within the sandbox environment, there are two dropdown menus labeled as From and To. ...

Opt for utilizing a global npm package over repeatedly installing it

Is there a way to utilize the globally installed package without needing to reinstall it every time we run npm i? Here's the scenario I have: I've set up a docker image with a specific package already installed (installation command in the Docker ...

The div elements are constantly shifting positions when viewed in Internet Explorer

After completing my website and testing it across different browsers, I noticed an issue with IE. The contact div and navigation shift around the page in this browser specifically. Can anyone help me out with some code to fix this problem? Link to live ex ...

How to process JSON data that includes a function

I'm trying to replicate a similar diagram using dynamic input data. Here is a basic example of how I'm attempting to achieve this: <script> var myYears = ', 1991, 1992, 1993, 1994, 1995, 1998'; //auto-generated var myValues = ...

Choose the div element that is immediately before the current div

Attempting to create a harmonica effect by adjusting the height of divs upon hovering over another. The HTML is structured as follows: <html><body> <div class="section1"></div> <div class="section2"></div> <di ...

Leveraging Google Cloud Functions with Next.js (Client-Side Rendering)

Having trouble incorporating firebase cloud functions into my Next.js project, encountering an unfamiliar error. firebase-config.js const firebaseConfig = { apiKey: '~~~', authDomain: '~~', projectId: '~~~', storageBu ...

"Upon completing the AJAX file upload, the response message fails to appear on the

I am currently diving into the world of ajax and attempting to create a file uploader using ajax in codeigniter. The issue I am facing is that although the file gets successfully uploaded to the /uploads folder, the response message (alert or image preview ...

Top choice for removing items from a list array

Hey there, I'm working with an array of a custom type in Angular: List { task: string; id?: number; status?: boolean; } I'm trying to figure out how to delete elements where List.status == true. I've tried two methods for this. ...

Adjust Javascript to modify the URL by assigning a new URL without utilizing the origin as the parent

Currently, I am working on a script that is responsible for sending string values to the server in order to change the ipv4 information on a specific device. However, I am facing some difficulties and would appreciate some assistance with automatically upd ...

Why do certain servers encounter the "Uncaught SyntaxError: Unexpected token ILLEGAL" error when loading external resources like Google Analytics or fonts from fonts.com?

Working on a variety of servers, I encountered a common issue where some externally loaded resources would throw an error in Chrome: "Uncaught SyntaxError: Unexpected token ILLEGAL". While jQuery from the googleapis CDN loads without any problems, attempt ...

The $resource.query function will now return an array of characters instead of a single string when splitting strings

Recently, I've been working on utilizing an Angular $resource in my project. Here's a snippet of the code: angular.module('app') .factory('data', function ($resource) { var Con = $resource('/api/data', {}, { ...