I am using this script to center my div horizontally and vertically.
When the page loads the div gets centered vertically, not horizontally until I resize the brows
Related to your code snippet, you need to set the position first:
$(window).resize(function (){
var $el = $('.className');
$el.css('position', 'absolute').css({
left: ($(window).width() - $el.width()) / 2,
top: ($(window).height() - $el.height()) / 2
});
});
Maybe you could set the position attribute via a static CSS style.