I'm trying to set the margin-top and margin-left values to half of an element's respective size so as to center it. However it just goes 50% on the left and top.
var $img = document.getElementsByClassName('box');
var $width = $img.clientWidth;
var $height = $img.clientHeight;
$('#overlay').append('<div id="boxOverlay"></div>');
$('#boxOverlay').css('position', 'fixed');
$('#boxOverlay').css('top', '50%');
$('#boxOverlay').css('margin-top', '-$height/2');
$('#boxOverlay').css('left', '50%');
$('#boxOverlay').css('margin-left', '-$width/2');
$('#boxOverlay').css('max-height','80%');
$('#boxOverlay').css('max-width','90%');
$('#boxOverlay').height($height);
$('#boxOverlay').width($width);