Ensuring the footer sticks to the bottom when the body height is set to 100%

I prefer to design my styles based on the height of the window, which I achieve by setting the html and body elements to a height of 100%.

html, body{
   min-height: 100%;
   height: 100%;
}

From there, I can customize the heights of other elements accordingly using the approach demonstrated in the jsfiddle link below. However, I encounter an issue with getting the footer to stick to the bottom. While it works fine for pages that don't extend beyond the window height, the footer becomes fixed at the bottom when they do. How can I ensure that the footer always sticks to the bottom while still utilizing the 100% height set for html and body? Notably, I tried using the wrapper method as shown here:

#wrapper{
  min-height:100%;
}

However, this didn't work well because of issues with the height of child elements.

http://jsfiddle.net/mmahalwy/m4AjL/

Any suggestions?


Edit: I was able to resolve this issue by implementing the following solution: http://jsfiddle.net/mmahalwy/7wqqF/

Thank you, everyone!

Answer №1

Let's address the issue:

The current setup involves two CSS style tags with a height of 100% each, identified as class=block. However, by creating a main div with a height of 100% and nesting all other divs within it, the problem can be resolved.

Answer №2

<body>
    <container>
        <div class="block">
             <h1>Unique Content</h1>

        </div>
        <div class="block">
             <h1>Different Content</h1>

        </div>
    </container>
    <footer>This is a unique footer</footer>
</body>

html, body {
    min-height: 100%;
    height: 100%;
}
container {
    background: grey;
    display: block;
    height: 100%;
    width: 100%;
}
.block {
    height: auto;
}
footer {
    background-color: blue;
    color: white;
    position: relative;
    bottom: 0px;
    width: 100%;
}

Answer №3

Choosing the right approach depends on the level of browser compatibility required.

In my opinion, utilizing CSS3 flexbox is the most effective modern method. Check out this informative guide from css-tricks.com: http://css-tricks.com/snippets/css/a-guide-to-flexbox/.

You can find the code in this JSFiddle: http://jsfiddle.net/m4AjL/24/.

Here is an example of HTML:

<body>
    <div class="container">
        <div class="block">
            <h1>Content</h1>
        </div>
        <div class="block">
            <h1>Content</h1>
        </div>
    <footer>I am a footer</footer>
    </div>
</body>

And here is the CSS:

html, body{
    min-height: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}
body{
    display: flex;
    flex-direction: column;
}
.block{
    display: block;
    height: 100%;
}
footer{
    background-color: blue;
    color: white;
    width: 100%;
    flex: none;
}
.container{
    flex: 1;
    overflow-y: auto;
}

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

Symfony allows for the addition of an embedded collection prototype with an empty string

It seems like I must be overlooking something simple. I'm currently referencing this guide http://symfony.com/doc/current/cookbook/form/form_collections.html and aiming to include a link/button for adding more filters to a brand. However, the data-pr ...

Issue with resetting the form field

Whenever a user opens a modal window to save data, I reset the form fields to blank. This works as expected, but I encountered an issue with AngularJS form validation messages appearing due to dirty check. I tried adding $setPristine() to resolve this, but ...

One controller, divergent template, introducing a fresh variable

I have a webpage where I showcase data (www.mysite.com/:gameId). In order to create a printer-friendly version of this data, I've set up a print page at www.mysite.com/:gameId/print. Both pages display the same information but with different designs. ...

Looking to implement a scroll bar in your PhoneGap mobile app for Android?

Encountering an issue with my Android PhoneGap mobile website application. I have implemented a scroll bar in the application which works perfectly on PC. However, when testing on an Android device (mobile phone), the scroll bar does not enable. ...

Having trouble getting the toggle button JavaScript code to function properly in React JS?

I'm in need of some assistance with the comment section located below the JavaScript Code. I am trying to implement a toggle button / hamburger button, but it seems that addEventListener is not necessary for ReactJS. Can someone provide guidance on wh ...

The element in TS 7023 is implicitly assigned an 'any' type due to the fact that an expression of type 'any' is not valid for indexing in type '{}'

I have implemented a select-box that includes options, labels, optgroups, and values. Is my approach correct or is there something wrong with the way I have defined my types? interface Option { label: string value: string selected?:boolean mainGrou ...

Failure to receive Ajax XML data in success callback

I am struggling to access the book.xml file that is located in the same folder as other files. Everything seems fine, but the ajax function refuses to enter the success state and instead shows an [object object] error message. The XML file is very simple, ...

Determining the size of the axis in correlation to a set constant length

Basic Concept I am currently developing a small tool designed to assist with geometric calculations for print-related items. Project Overview In this tool, users are asked to input the width and height of a box, represented visually as a CSS-based box, ...

Unable to maintain the height of a div at 100% as it keeps reverting back to 0px

I'm having trouble setting the height of the individual parent columns (and their children) to 100% because for some reason, the div height keeps resetting to 0. The first column contains two child boxes that should each take up 50% of the page heigh ...

Tips for managing a fixed-positioned element during scrolling and zooming events

I have a situation where I need a fixed-position element to stay at the top-right corner of the viewport. Here is the HTML code: <div class = "header"> <p>This heading has an absolute position</p> </div> And here is the CSS: .he ...

Importing text data from a text file in Java or jQuery without the need for a web server

I've attempted to utilize both the jQuery.Get() and $.Get() methods for this task, but they don't seem to be working as expected. <head> <script type="text/javascript" src="jquery-2.1.3.min.js" > </script> <script lang ...

Utilize Discord.js v13 to stream audio directly from a specified URL

Can anyone help me figure out how to play audio from a URL using discord.js v13? I attempted this code but it's not working as expected. const connection = joinVoiceChannel({ channelId: voiceChannel.id, guildId: message.guild.id, adapterCreator ...

What is the ideal format for an AJAX request that includes sections for handling success and error scenarios?

When making AJAX calls for CRUD operations in MVC, I often find myself unsure about the standard or most common usage of the complete, success, and error functions. Some examples show these functions without any parameters, while others include parameter ...

Sharing configurations between a Node.js application and client-side JavaScript

I am facing an issue where I need to use a config object in both my node app and browser. Below is the path and content of the configuration file: path: [app]/public/js/config.js content: var config = { "foo": "bar", "num": 42, "list": ["a" ...

Generate unique identifiers in HTML

Below is my HTML and JavaScript code: <!DOCTYPE html> <html> <head> <style> .custom-div { border: 1px solid green; padding: 10px; margin: 20px; } </style> <script> ...

Is it possible to adjust the font size in Bootstrap 4.6.2 to fit smaller fonts and bring back color to the tabs that were removed in the update?

While attempting to update Bootstrap from version 4.6.0 to 4.6.2 in a few codebases, I encountered some issues that prevented me from completing the upgrade successfully. The <small> tag and .small CSS class are now displaying larger than before. Ta ...

Optimal method for retrieving data from a JSON object using the object's ID with a map

Can you teach me how to locate a json object in JavaScript? Here is a sample Json: { "Employees" : [ { "userId":"rirani", "jobTitleName":"Developer", "preferredFullName":"Romin Irani", "employeeCode":"E1", "region":"CA", "phoneNumber":"408-1234567", " ...

Is there a way for me to retrieve and view the specific data that jQGrid is receiving from the controller?

I have a unique attribute that verifies if the user is authenticated. If not, a special JSON string is returned. Therefore, I need to search for that specific string in the data returned from the controller after making a request with jQGrid. How can I ach ...

Expand the capabilities of a jQuery plugin by incorporating new functions or modifying existing ones

I have a task to enhance the functionality of a jQuery Plugin (https://github.com/idiot/unslider) by adding another public method. (function(){ // Store a reference to the original remove method. var originalMethod = $.fn.unslider; // Define a ...

What's the best way to arrange the layout to ensure that every row contains exactly three items that are evenly spaced apart?

Struggling to figure out how to display three items in each row with even spacing using react js and bootstrap. I've been grappling with this for hours today, trying to implement grid layouts on the page. I managed to render 3-4 items on each row, bu ...