简体   繁体   中英

set padding top of div to minus the height of the header

I can set padding to my div with

<div id="Products" style="background-color:red; width:100%; padding-top:70px">
</div>

but instead of using 70px is it possible to use a value I have generated from the size of the header...

<script>
    function resizeDiv() {
    var headerHeight = $('header').outerHeight();
    }
</script>

So set top padding to headerHeight....is this done in css or js?

Use the css() function.

You can create a function that runs on load and (if you want) on resize if the header changes it's height:

var setHeight = function() {
  var top = $('header').outerHeight();
  $('#products').css({'padding-top': top + 'px'});
}

$(window).load(function() {
  //On load you can be sure that the target element has been loaded 
  //(except if it is loaded from an ajax call)
  setHeight();
});

$(window).resize(function() {
  setHeight();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM