Best way to center a div on a page both vertically and horizontally?
I know that: margin-left: auto; margin-right: auto; Will center on the horizon but what is the best way to do it vertically too?
Thanks
Best way to center a div on a page both vertically and horizontally?
I know that: margin-left: auto; margin-right: auto; Will center on the horizon but what is the best way to do it vertically too?
Thanks
Here is a script i wrote a while back (it is written using the jQuery library):
var centerIt = function (el /* (jQuery element) Element to center */) {
if (!el) {
return;
}
var moveIt = function () {
var winWidth = $(window).width();
var winHeight = $(window).height();
el.css("position","absolute").css("left", ((winWidth / 2) - (el.width() / 2)) + "px").css("top", ((winHeight / 2) - (el.height() / 2)) + "px");
};
$(window).resize(moveIt);
moveIt();
};