These position: absolute;
divs placed inside/around a position: relative;
parent receive height/width from the size and length of the contained text.
However, if one of them extends beyond the right border of the relative parent, its width appears to truncate.
How can I make the div which extends beyond the right side of the relative parent behave as the others which don't, using only css? (Should work on all major browsers including Edge but not IE)
Here is a fiddle, but I will also copy the code + screenshot below:
HTML:
<div id="relative">
<div id="absolute-working-left">
My width and height are determined by my text content.
</div>
<div id="absolute-working-middle">
Mine, too.
</div>
<div id="absolute-problem">
But not me... I am a problem, because my width is truncated when I extend beyond the right side of my 'relative' parent. WHY???
</div>
</div>
...and the styles. Note that I want the absolute div width to reach a max of 200px before wrapping. Otherwise, the width should be determined by the length of the contained text:
#relative {
position: relative;
width: 100px;
height: 100px;
margin-left: 300px;
background-color: white;
}
#absolute-problem,
#absolute-working-left,
#absolute-working-middle {
position: absolute;
top: 45px;
font-size: 12px;
max-width: 200px;
}
#absolute-working-left {
background-color: lightblue;
left: -300px;
}
#absolute-working-middle {
background-color: lightgreen;
}
#absolute-problem {
background-color: red;
left: 80px;
}
This issue arose while implementing a tooltip which, when used by a developer, needs to position itself WRT an offsetParent
(or the body, if no preceding offsetParent
exists in its DOM branch).
Edit: How can I achieve the desired behavior using only css?
The solution needs to work on all major browsers including Edge, but not IE. And just to reiterate, the width
of the absolute divs' cannot be predicted as it should be determined by the length of the texts... the only exception being that there will be a max-width
(in this example, it is 200px).