Bootstrap 4 - Sticky footer - Dynamic footer height

落爺英雄遲暮 提交于 2019-11-27 02:07:22

Now that Bootstrap 4 is flexbox, a sticky footer can be done using...

<wrapper class="d-flex flex-column">
    <nav>
    </nav>
    <main class="flex-fill">
    </main>
    <footer>
    </footer>
</wrapper>

body, wrapper {
   min-height:100vh;
}

.flex-fill {
   flex:1 1 auto;
}

Demo: Bootstrap 4 Sticky Footer (4.0.0)

Note: The flex-fill utility class will be included in the next Bootstrap 4.1 release. So after that release the extra CSS for flex-fill won't be needed.

Use min-height instead of height to make it variable height.

https://codepen.io/hunzaboy/pen/prxgRb

Also on smaller screens just remove the absolute position and make it static.

A very simple way is using a navbar as a footer. It comes with a fixed-bottom class that is really handy. To change your spacing you can see the documentation here... https://getbootstrap.com/docs/4.2/utilities/spacing/

<nav class="navbar fixed-bottom navbar-light bg-light">
  <a class="navbar-brand" href="#">Fixed bottom</a>
</nav>

This script dynamically add fixed-bottom class if page height less than viewport height:

jQuery(function ($) {
  function fixedFooter(){
    var $footer = $('.site-footer');
    if(typeof $footer === 'undefined') return 0;
      $footer.removeClass('fixed-bottom');
      if($(window).height() > $body.height()){
        $footer.addClass('fixed-bottom');
      }
    }

    // call `fixedFooter()` when page height changed (not by viewport resize)
    if(typeof window.lastPageHeight === 'undefined'){
      setInterval(function () {
        if(window.lastPageHeight !== $body.height()){
          fixedFooter();
        }
      }, 1000);
    }
    window.lastPageHeight = $body.height();
  }

  fixedFooter(); // first call

  // call `fixedFooter()` when viewport height changed
  window.addEventListener('resize', function(){
    fixedFooter();
  }, true);

});

Style for prevent overlapping with modals, fixed sidebars etc...

.site-footer.fixed-bottom {
  z-index: 1 !important;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!