Ryan Fait's Code for Creating a Responsive Sticky Footer Height

There are quite a few queries regarding Ryan Fait's sticky footer, but I urge you not to dismiss this one immediately! My goal is to have a footer with a dynamically sized height that sticks to the bottom of the page.

Ryan Fait suggests wrapping all page content in #wrapper and setting the margin-bottom of #wrapper to match the height of the footer. While this technique works when the height is hardcoded in the CSS, I am unsure of the exact size of my footer. Therefore, I aim to use JavaScript to set #wrapper's margin-bottom. However, my attempts so far have been unsuccessful. You can view my setup here.

I have spent so much time on this puzzle that I'm running out of hair to pull. Can you spot where I might be going wrong?

The Code on JSFiddle

HTML

<body>
    <div id="wrapper">
   Wrapper
    </div> <!-- #wrapper -->

    <footer>
        Footer
    </footer>
</body>

CSS

* {
    margin: 0;
    }
html, body {
    height: 100%;
    }
#wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto; /* Negative margin set with JS */
    }
footer {
    height: 100px;
    }

JQuery

$(window).load(function() {
    set_window_bottom_margin();
});

function set_window_bottom_margin() {
    var margin = $('footer').outerHeight(true);

    $('.wrapper').css('margin-bottom', margin * -1 + 'px');
}

Answer №1

One way to achieve this effect is through CSS:

html, body {
  height:100%;
  width:100%;
  margin:0;
}
body {
  display:table;
}
main, footer {
  display:table-row;
}
main {height:100%;background:red;}
footer {background:green;}
footer:hover br {display:none;}/* Additional styles for demo purposes */
<main>
  <div>
    Main Content
  </div>
</main>
<footer>
  <div>
    Footer Text<br/>
    Footer Text<br/>
  </div>
</footer>

Check out the DEMO here

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

Encountering difficulties accessing Node.JS Sessions

Hey there, I am currently working on integrating an angular application with Node.js as the backend. I have set up sessions in Angular JS and created my own factory for managing this. Additionally, I am utilizing socket.io in my Node.js server and handling ...

The user interface does not get refreshed right away; it only shows the changes after the

Here is an example of HTML: <div ng-repeat="user in controller.users"> <p>{{user.name}}</p> <button ng-click="controller.deleteUser(user)" value="delete"></button> </div> Next, we have the controller code: vm ...

Combining CSS and HTML with IE10

With the release of Windows8 and IE10 as developer previews, I am curious about what features IE10 does not support that IE9/8 did, as well as what new features it now supports. Thank you for your assistance! ...

Achieve full parent width with floated divs

My goal is to arrange 10 boxes horizontally on a webpage. Each box must have a minimum width of 150px and a maximum width of 299px. The challenge is to fit as many boxes as possible across the page without any gaps, ensuring that each box has an equal widt ...

There appears to be a few bugs present in my html code

I'm uncertain about what I am missing, can someone help me out? Thank you. When I click "check," it returns wrong(); It should actually return yes(); because both have the same value and result. However, it's only giving wrong(); If the value ...

The effective method for transferring a PHP variable value between two PHP files using jQuery

I am looking to transmit the variable "idlaw" from base.js to the PHP page edit.php. modifyLegislation.php <input class='btn btn-primary buttonBlue' type='button' name='btnAcceptPending' value='Edit' onClick="ja ...

What is the best way to integrate a vh property instead of px for a scrolling navigation using jQuery?

I want to achieve a navigation bar that fades in after scrolling 100% of the viewport height, but all examples I find use a pixel value instead of viewport height. Is there a way to convert window height into jQuery to make this work? Thank you in advance ...

What is the best approach to configure Nuxt.js to recognize both `/` and `/index.html` URLs?

Currently, I have set up my Nuxt.js in default mode with universal and history router configurations. After running nuxt generate, the generated website includes an index.html file in the dist folder. This means that when the website is published, it can ...

React.js issue with onChange event on <input> element freezing

I am experiencing an issue where the input box only allows me to type one letter at a time before getting stuck in its original position. This behavior is confusing to me as the code works fine in another project of mine. const [name, setName] = useStat ...

Transferring information to React (Native) hooks in a way similar to how it is done

Transitioning to Hooks in React Native has been a new learning curve for me, especially when it comes to passing data to child elements. In the past with class-based components, passing props was as simple as using XML-style syntax. A Simple Example using ...

Utilizing dynamically assigned ng directives in my unique custom directive

I'm in the process of creating a customized table element that looks like this: <datatable items='tableItems' columns='columnsConfig' /> Here, 'tableItems' represents my array of items and 'columnsConfig&apos ...

Modify the JQuery change function depending on whether the 'select' exceeds or falls below a specified value

Currently, I am facing a challenge that I believe is not too far from being solved. Essentially, I have an Age Verification form that only displays the year unless the year would make a difference in meeting the age requirement, in which case the day and ...

bespoke JavaScript confirmation dialogue box

After customizing a confirmation box for the logout feature, I encountered an issue. When the user presses cancel, the box closes and control remains on the same page as expected. However, when the user clicks yes to logout, nothing happens. Could anyone p ...

Exploring logfile usage in JavaScript. What is the best way to structure the log?

Currently, I am developing a Python program that parses a file and records the changes made to it. However, I am facing a dilemma regarding the format in which this information should be saved for easy usage with JavaScript on the local machine. My objecti ...

Having trouble applying CSS to multiple classes used in an AJAX call

I'm trying to utilize richfaces for some fast AJAX widgets, but I am encountering difficulties in setting CSS parameters for them. After inspecting the generated code, I found that the elements have the classes "rf-ds" and "rpds". Despite applying st ...

Tips for recognizing when Vuetify's v-autocomplete has reached the final scrolled item

I am working with a Vuetify v-autocomplete component and I need to implement a feature where I can detect when the user reaches the last item while scrolling, in order to load more items without the user having to manually type for search: // component.vue ...

Decrease in font size observed after implementing Bootstrap 5

The issue arises when I include the Boostrap CDN link, resulting in a change in font size. I discovered that Bootstrap has a default font size, which is why attempts to adjust it using an external style sheet with !important do not succeed. Interestingly, ...

What is the method for accessing JSON data?

Looking for assistance on a coding issue I'm facing. I need my JavaScript code to read JSON data and grant access to a staff panel based on the information provided. Below is the sample JSON: { "User1": { "User": " ...

What is the best way to retrieve the transpiled string from babel-core?

I've been attempting to utilize babel with npm and it seems like the necessary package is babel-core. My goal is to provide it with a string of ES6 code and receive a transpiled code string in return. It sounds simple enough, but I'm having troub ...

Integrate SVG into the dropdown menu without duplicating the SVG element

I have an SVG of a right arrow: <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <path d="M0 0h24v24H0V0z" fill="none"/> ...