Vertical space on elements with position:absolute

Yisela picture Yisela · Aug 7, 2012 · Viewed 25.9k times · Source

How can I make elements with position:absolute and dynamic height occupy vertical space using only css? Is there any trick with containers and display that I can use?

Answer

Jeff Gortmaker picture Jeff Gortmaker · Aug 7, 2012

Unfortunately, using absolute positioning means, by definition, that your element is no longer taking up space. So no, only through css there is no way to do this.

You can of course use jQuery (or plain javascript) to accomplish this. How I'd do it is have a space element next to each vertical element. Enclose both the space element and the absolutely positioned vertical element in a relatively positioned div. On page load, change the height of the space element to match the height of the vertical element.