Home:ALL Converter>Issue On Setting Element CSS Left By Using jQuery

Issue On Setting Element CSS Left By Using jQuery

Ask Time:2014-06-14T07:37:28         Author:Suffii

Json Formatter

This code simply using a .css() in jQuery to center element is the page. This is setting the vertical centering but the horizontal (left) is not working (Please be ware that I already knew there is a text-align:center rule in CSS to to do this by element parent, but I really need to know why jquery is not doing now?!)

var elemntHeight = $("h1").height();
var elementWidth =  $("h1").width();

var windowHeight = $(window).height();
var windowWidth  = $(window).width();

$("h1").css({
    "position" : "absolute",
    "left" :  windowWidth /2 - elementWidth/2,
    "top":  windowHeight/2 - elemntHeight /2
});

can you please let me know how to fix this?

Author:Suffii,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24215137/issue-on-setting-element-css-left-by-using-jquery
yy