here's a stackoveflow that I adapted the accepted answer's code from for my own purposes a while back. Here's what I have to position to the window or to the parent element:
Using jQuery to center a DIV on the screen
Demo: http://jsfiddle.net/6P7AM/1/
jQuery.fn.center = function (centerToWindow) {
var parent = null;
var positionCss = null;
if (centerToWindow) {
parent = window;
positionCss = "absolute";
} else {
parent = this.parent();
positionCss = "relative";
}
this.css({
"position": positionCss,
"top": Math.max(0, (($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop()) + "px",
"left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px")
});
return this;
}
$(function () {
$(window).resize(function () {
$("#gallery-container").center(true);
}).trigger("resize");
});