Dynamically Adjust Div Margin-Top Based on Size of Fixed Navigation

Single fixed navigation solution:

jQuery('#main').css("margin-top", function() { return jQuery('#nav').outerHeight() });

Multiple fixed navigation bars:

var outerHeight = 0;
jQuery('#nav').each(function() {
	outerHeight += jQuery(this).outerHeight();
});
//jQuery("#total-height").text(outerHeight + 'px');

jQuery('#main').css("margin-top", function() { return outerHeight + 50 });

Reference for multiple fixed navigation.

Used to dynamically resize divs in sequence to all be the same height (on load, and on window resize):

jQuery('.container').css("min-height", function() { return jQuery('.container').width() * 2 } );
   jQuery(window).resize(function(){
       jQuery('.container').css("min-height", function() { return jQuery('.container').width() * 2 } );
   });

This comes in handy when loading inline background images on divs.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.