Okay, so I have a div and I want to position it in the middle of a page. I've gotten so far
$("#a").css('margin-top', '(document).height()/2 - ("#a").height()/2');
Is this correct?
I suggest you use .offset()
.
Description: Set the current coordinates of every element in the set of matched elements, relative to the document.
$("#a").offset({
top: $(document).height()/2 - $("#a").height()/2,
left: $(document).width()/2 - $("#a").width()/2
})
**It shouldn't be in quotes. Also, you need to use the $()
terminology. Try this:
$("#a").css('margin-top', $(document).height()/2 - $("#a").height()/2);
Or even better:
var $a = $("#a");
$a.css('margin-top', $(document).height()/2 - $a.height()/2);
Edit: Just to be clear, you can't put it in quotes because it will try to set the margin-top property literally to that string. Which is incorrect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With