Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get distance between two divs

So I have one div inside the other - how can I get distance between them?

buttons

I tried something like $('#child').parentsUntil($('#parent')).andSelf() - but it returns an object, not a distance.

P.S. I need it to push other buttons.

like image 981
NoNameZ Avatar asked Oct 01 '12 09:10

NoNameZ


People also ask

How can I find the distance between two divs?

The easiest one is to simply place </br> element between them in the code in case they are placed in one column one above the other. The other way is to use padding values. To add a margin and create space around an image you will have to add custom style to an image element.

How do you find the distance between two elements in HTML?

function getPositionAtCenter(element) { const {top, left, width, height} = element. getBoundingClientRect(); return { x: left + width / 2, y: top + height / 2 }; } function getDistanceBetweenElements(a, b) { const aPosition = getPositionAtCenter(a); const bPosition = getPositionAtCenter(b); return Math.

How do you put a space between two vertical divs?

To give to space between two divs, just add display: flex in the parent element. Then add an justify-content: space-between after adding this you will see a space between two divs.


1 Answers

http://api.jquery.com/position/

to get the left distance you can use:

var distLeft = $('#child').position().left; 

That will return the distance in px relative to the offset parent

if you're interested into the element's page offset than:

var offsLeft = $('#child').offset().left;

http://api.jquery.com/offset/

like image 114
Roko C. Buljan Avatar answered Nov 03 '22 00:11

Roko C. Buljan