Positioning an Element Relative to Another Element - J Query

You want to position a new element relative to an existing element.

Get the width, height, and offset of the existing element, and use the values to position the new element accordingly.Given the following HTML:

the following code would add an element as a sibling to the existing element but positioned “inside” the element, 10 pixels from the top and 10 pixels from the left of the existing element’s top-left corner, and with a width 20 pixels less than that of the existing element:

If you wanted to add the new element somewhere else in the page—that is, if you didn’t want it to be a sibling of the existing element—you could adjust your code to look at the offset of the original element rather than the position:


J Query Topics