Skip to content
Advertisement

How to position a div at the top of another div

The first div is the '#icon-selection-menu' bar, it’s idle position is absolute with top:0px and left:0px. So it appears at he top left corner inside the content div.

Deep in the content div children I got other divs which are actually kind of ‘.emoticon-button’. Their position is relative inside their parent. On such button click I’d like to position the first div just above the button, adjusting it’s bottom border to the button’s top border.

How can I get top and left values to set $('#icon-selection-menu').top and $('#icon-selection-menu').left ?

Advertisement

Answer

jQuery1 provides .offset() to get the position of any element relative to the document. Since #icon-selection-menu is already positioned relative to the document, you can use this:

var destination = $('.emoticon-button').offset();
$('#icon-selection-menu').css({top: destination.top, left: destination.left});

$('#icon-selection-menu') will be placed at the top-left corner of $('.emoticon-button').

(1) jQuery assumed due to the use of $ in the question.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement