Adjust the height of the div to match the height of the screen and ensure the footer is fixed

I'm facing a problem on my website where the footer moves up when there isn't enough content, causing it to not stick to the bottom. I am trying to find a solution to make the div between the footer and header stretch to fill the available space, ensuring that the footer remains at the bottom without any layout issues.

So far, I have attempted setting the heights to 100% but unfortunately, it did not work as expected.

Here is the relevant HTML code snippet:

<div id="wrapper">

    <div id="body_div">
        <section id="main_section">
        </section>
        <aside id="sidebar">                    
        </aside>
    </div>
    <footer id="footer">
        &copy; Copyright  by SimKessy
    </footer>
</div>

And here is the corresponding CSS code snippet:

body{
    width: 100%; 
    height: 100%;
    display: -webkit-box;
    display: -moz-box;
    display: box;
    text-align: center;
    -webkit-box-pack: center; 
    -moz-box-pack: center;
    box-pack: center;
    background:url('images/bg/bg9.jpg') no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
}
#wrapper{
    max-width: 850px;
    display: -moz-box;
    display: -webkit-box; 
    -moz-box-orient: vertical;
    -moz-box-flex: 1;
    -webkit-box-orient: vertical;
    -webkit-box-flex: 1; 
    background: rgba(0,0,0,0.7);
    height: 100%;    
    z-index: 1;
}
#body_div{
    display: -webkit-box;
    -webkit-box-orient: horizontal;
    display: -moz-box;
    -moz-box-orient: horizontal;
    color: #000000;
    margin-top: 190px;
    height: 100%;
}
#main_section{
    -webkit-box-flex: 1; 
    -moz-box-flex: 1;
    margin: 20px;
    padding: 0px;
    height: 100%;
    width: 100%;
}

You can visit my site here to see the issue in widescreen mode using the side menu.

Answer №1

You are on the right track towards finding the answer. However, simply setting the #wrapper div to 100% height is not enough. It is essential to also include the following line:

html, body, #wrapper {
width:100%; 
height:100%;
}

The current issue lies in the fact that the wrapper div does not have a frame of reference for what constitutes 100%. This reference needs to come from the parent elements such as html and body.

For achieving a sticky footer effect, utilizing absolute positioning and setting bottom:0px; is recommended. Avoid resorting to a third-party API for this task; opting for position:absolute is a straightforward solution, while integrating a third-party API may negatively impact your website's performance.

Answer №2

To dynamically calculate the necessary height using jQuery, follow these steps:

  • Ensure that the margin for the body is set to 0
  • Then execute the script below:

<script type="text/javascript>
    $(document).ready(function(){
        var clientHeight = document.documentElement.clientHeight;
        $('#wrapperHeight').height(clientHeight+'px');
        var bodyHeight = clientHeight - $('#body_div').css("marginTop").replace('px', '') - $('#footer').outerHeight(true);
        $('#body_div').height(bodyHeight+'px');
    });
</script>

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

Creating a mobile-friendly table using Bootstrap 4 and CSS: A step-by-step guide

Looking to create a responsive table? I've got my table ready for desktop view, but now I want it to display properly on smartphones too. Here's how my table looks on the website: https://i.sstatic.net/zMXQo.png and here's the code I&apos ...

Is there a way for me to achieve a vertical page turning effect on a single page?

I am seeking to develop a unique calendar display consisting of 12 images that give the illusion of flipping up when clicked. While I am aware of turn.js, my knowledge of javascript is limited and I need guidance on how to proceed. Despite having a program ...

In CSS, there are two dynamic fields with adjustable widths, each occupying half of the total length

I have a collection of email addresses and names displayed in rows. My goal is to maximize the amount of content shown. For short names, more of the email address should be visible, while for shorter email addresses, more of the name should be shown. If bo ...

Need to convert an HTML table to JSON format? Visit convertjson.com/html-table-to-json.htm to easily convert your HTML table

Can anyone help me with converting HTML table data to JSON? I found a website that does something similar: I have tried some solutions but encountered issues at the end. I just need a JSON output to convert it into an array and continue with my project. ...

Canvg | Is there a way to customize canvas elements while converting from SVG?

Recently, I encountered an issue with styling SVG graphics dynamically generated from data. The SVG graphic appears like this: https://i.sstatic.net/xvIpE.png To address the problem, I turned to Canvg in hopes of converting the SVG into an image or PDF us ...

Using HTML to create an email link with a subject that includes special characters such as '&'

Is it possible to dynamically set the email subject to include an '&' sign in client-side HTML? Here is the code I am working with: <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="533d3c7e3c3d361320 ...

Class without a single TD in TR

<table> <tr class="here"><td><input type="text" readonly=readonly></td></tr> <tr class="here"><td><input type="text" readonly=readonly></td></tr> <tr class="here"><td> ...

the symbol used to denote a list item and the actual content within the list item

When I use the following code, only the last list item is colored blue while the list sign remains uncolored: <font color="blue"> <li>man <li>woman <li>body <li>girl <li>baby ...

I'm interested in finding out how to utilize the bezierjs library. Can someone

I am working with a canvas drawing that features various curves, and I am curious about determining its size similar to a specific example in this library. Check out the library here For reference, take a look at this example: Curve Size Example I would ...

Position the footer at the bottom of the content and shift it to the right of the sidebar

I was aiming to position the footer at the bottom of the content, without extending it across the entire width of the page. The footer should always be aligned with the bottom if the content is not filling up the space, as illustrated in figure 1. The side ...

Tips for handling an incorrect date entry when a field loses focus in AngularJS

If I have an <input> field with the type='date' attribute in its untouched state, it displays mm/dd/yyyy as the date format. I am looking to see if there is a way to utilize AngularJS ng-blur directive to reset the field to this original fo ...

How do you center an inline-block div inside its parent div using CSS?

(Code: http://jsfiddle.net/T4hrK/) I have been searching endlessly, but cannot seem to find a solution. I have an outer div, and within it, I wish to center an inner div. This inner div contains numerous inline elements (such as images in a gallery) that ...

Using javascript to quickly change the background to a transparent color

I am in the process of designing a header for my website and I would like to make the background transparent automatically when the page loads using JavaScript. So far, I have attempted several methods but none seem to be working as desired. The CSS styles ...

Creative approach to achieving responsive borders with Bootstrap 5

Looking to create a dynamic border using Bootstrap 5 that will hide at specific breakpoints. I tried the following code: <p class="border-bottom border-lg-0-bottom border-lg-end">text here</p> Unfortunately, this code did not work a ...

Mobile-friendly HTML email design featuring bottom navigation bar

Currently, I am in the process of coding a responsive email template. Here is what I have so far: Click here to view <tr class="main_nav"> <td id="mobile_nav" width="600"> <table cellpadding="0" cellspacing="0" width="100%"> ...

The challenge of removing an event listener

There are three Div elements with a box appearance. When the user clicks on any div, a copy of that div will be added to the end. The original clicked div will no longer be clickable, but the new div will be. This process can repeat indefinitely. I attemp ...

Employing the CSS not selector within JavaScript

I am facing an issue where my form darkens with the screen every time I click a button to show it, using JavaScript. If I were using CSS, I know I could use the :not selector, but I need guidance on how to achieve this in JS. Can anyone assist me? Below i ...

Creating a div that becomes fixed at the top of the page after scrolling down a certain distance is a great way to improve user experience on a

I am struggling to create a fixed navigation bar that sticks to the top of the page after scrolling 500px, but without using position: fixed. Despite trying various solutions, none seem to work due to the unique layout of my navigation bar. Strangely enoug ...

What could be causing my centrally-aligned divs to not scroll horizontally?

(Please note that there are no scrollbars on these divs, so scrolling to the left is not possible within the body or the divs themselves.) The issue (shown above in FF and Opera) occurs on both the "Baroque" and "Spotlights" theme selections (refer to the ...

What methods can I use to eliminate the gap between a div and the page?

<!DOCTYPE html> <html> <head> <title>Functions</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width initial-scale=1"> </head> main ...