Search code examples
jquerycssposition

How to setup absolute position of a DIV with Jquery?


I have a code like that:

$('#lol').css({ position: "absolute",

        marginLeft: 0, marginTop: 0,

        top: 0, left: 0});

The problem is that my div is positioned relatively, so it is point 0 of a div rather than the entire window. Why is that?


Solution

  • As pointed out in the other answers, at least one of the parent element of #lol has a position set, that causes your element to be positioned within the parent.

    A solution with jQuery would be to attach the element directly to body.

    $('#lol').css({ 
        position: "absolute",
        marginLeft: 0, marginTop: 0,
        top: 0, left: 0
    }).appendTo('body');
    

    This will make it to appear top left of the window.