📜 ⬆️ ⬇️

Footer pressed to the bottom of the page

Pressing the footer to the bottom of the page, I think a good tone. After reviewing a lot of solutions, I found that all of them are based on any hacks and require that a bunch of garbage be inserted into the code. I propose a simple solution based on JavaScript, which does not require any additional elements and works stably in all browsers.

All you need is to connect a special script and assign the ndra-container class to the element to be stretched. Done!

See an example
')


A page with a pressed basement should look something like this:
< html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
  1. < html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
  2. < html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
  3. < html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
  4. < html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
  5. < html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
  6. < html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
  7. < html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .
< html > < body > < div > </ div > < div class ='ndra-container' > </ div > < div > </ div > </ body > </ html > * This source code was highlighted with Source Code Highlighter .


The script will stretch the content block so that the footer rests on the bottom of the page. The script checks the height of the block every second and responds quickly to resizing the window. Works in all browsers. Use :) Here is the code for the script itself:

  1. $ ( function () {
  2. $ ( "body" ) .css ({padding: 0, margin: 0});
  3. var f = function () {
  4. $ ( ".ndra-container" ) .css ({position: "relative" });
  5. var h1 = $ ( "body" ) .height ();
  6. var h2 = $ (window) .height ();
  7. var d = h2 - h1;
  8. var h = $ ( ".ndra-container" ) .height () + d;
  9. var ruler = $ ( "<div>" ) .appendTo ( ".ndra-container" );
  10. h = Math.max (ruler.position (). top, h);
  11. ruler.remove ();
  12. $ ( ".ndra-container" ) .height (h);
  13. };
  14. setInterval (f, 1000);
  15. $ (window) .resize (f);
  16. f ();
  17. });
* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/116267/


All Articles