最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - set padding top of div to minus the height of the header - Stack Overflow

matteradmin7PV0评论

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?

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?

Share Improve this question edited Jun 23, 2015 at 14:44 John asked Jun 23, 2015 at 14:42 JohnJohn 3,94523 gold badges84 silver badges174 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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();
});
Post a comment

comment list (0)

  1. No comments so far