dimanche 6 décembre 2015

Centering JQuery is not working right

I've got a function on Jquery that centers a div containing text, it looks like this:

function center_paragraph(){
    var w=$("#paragrah").width();
    var finalw=w/2;
    alert(w+"/2="+finalw); //Test
    $("#paragraph").css({
        'left':'50%',
        'margin-left':-finalw
    });
}

My problem is that the method width is returning me a bad value, something like 1000 while the div is around 600. I use this method because the div is on absolute position.

The div css:

 #paragraph{
    font-family:'Arial';
    font-size:4em;
    color:white;
    font-weight:bold;
    position:absolute;
    display:none;
    border:solid 1px white;

}

I don't do this in the CSS file because each screen can display the text in another size, so I think this is the correct way to calculate the center.




Aucun commentaire:

Enregistrer un commentaire