JohnQ
JohnQ

Reputation: 13

I can not center my div using jQuery script

I used this jQuery script to center my div based on browsers window resolution. However I can't get it working. What am I missing here?

I posted it in jsFiddle for better understanding here http://jsfiddle.net/9wvt3/

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}
$(#main).center();

Upvotes: 0

Views: 247

Answers (2)

Jared Farrish
Jared Farrish

Reputation: 49188

This:

$(#main).center(); 

Should probably be:

$('#main').center();

Note the ' in the $() function argument.

Upvotes: 5

Sparky
Sparky

Reputation: 98718

You are simply missing the quotes on #main within $('#main').center();

http://jsfiddle.net/9wvt3/2/

Upvotes: 3

Related Questions