Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of Absolutely Positioned Child Div with Inline-Block Parent Div

I've noticed some weird behavior on Chrome/FF/IE, and was wondering if anybody knew why HTML/CSS gets rendered in this way.

If you have an absolutely positioned child div, whose parent's display is set to inline-block, any white space in the child div is treated as a line break. This is true even if the child element is set to contenteditable="true" (when you try to type spaces, it causes a line break).

Here is a fiddle of the phenomenon: http://jsfiddle.net/cnPAG/1/

HTML:

<div id="container">
    <div id="content">Hello, World!</div>
</div>

CSS:

#container {
    display: inline-block;
    position: relative;
}
#content {
    position: absolute;
}

If you either remove the fact that it is absolutely positioned, or the fact that the parent element is inline-block, the problem is fixed.

like image 646
Camden Reslink Avatar asked Jun 09 '13 01:06

Camden Reslink


1 Answers

When you give absolute positioning to the child div (#content), you're removing it from the flow of the document, and therefore the parent div (#container) collapses, as it behaves as if it no longer contains anything, essentially having zero width or height. This causes the absolutely positioned child div (#content) to collapse. You can see a similar result if you remove your rules and give the parent a width:0 rule.

#container {
    width:0px;
}

jsFiddle example

like image 68
j08691 Avatar answered Oct 14 '22 09:10

j08691