So I have one div inside the other - how can I get distance between them?
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With