Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical space on elements with position:absolute

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?

like image 373
Yisela Avatar asked Aug 06 '12 23:08

Yisela


People also ask

How do you center absolute position vertically?

To center an element both vertically and horizontally: position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; But if you would like to understand how I came to these solutions, read further for more explanation.

Does position absolute take up space?

Unfortunately, using absolute positioning means, by definition, that your element is no longer taking up space.

What happens when an element is positioned absolutely?

Absolutely positioned elements are removed entirely from the document flow. That means they have no effect at all on their parent element or on the elements that occur after them in the source code. An absolutely positioned element will therefore overlap other content unless you take action to prevent it.

How do you make an element center by position absolute?

If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).


1 Answers

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.

like image 114
Jeff Gortmaker Avatar answered Nov 11 '22 13:11

Jeff Gortmaker