Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How to determine how much vertical space is freed after an element is removed

I'd like to close a message <div> element with a transition that smoothly reduces the element's vertical occupied space to zero. After the transition ends, the element will be removed from the document. There should be no additional jump of the document after this. The whole closing process is initiated by JavaScript from a click event handler.

The message element already has a transition to transform: scaleY(0) so it visually collapses. I've implemented the space freeing with a transition to margin-bottom: -...px where the value is computed by the element's actual height. (I ignore height changes while the transition is running, these shouldn't occur in practice.)

With this, the margin around the element is not considered yet. The element will free the space of its border box nicely. But as it is removed, additional inter-element margins and paddings are removed as well and additional space is freed. The elements further down on the page suddenly jump up by that amount. This is an undesired effect.

And this is where things get complicated. The message element itself has a top and bottom margin of 1em. There might be a previous or next sibling element, or not. It might have its own vertical margin, more or less than the message, or not, and it may also be empty (which seems to totally not take part in inter-element margin calculations). The parent element may have a vertical padding or not. My message can be used anywhere. And it should always smoothly collapse exactly to disappearing.

How can this be done? How can I determine the additional space (to the element's own height) that I must add to my negative margin?

PS: To clarify the question title: I need to know that difference amount before actually removing the element.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!