I have a parent
container with a fixed size, and absolutely positioned child
divs inside it. The position of the child divs have dynamic text content and a max-width and can move around freely, and extend outside of the parent
, which is set to overflow: hidden
. See the snippet at the bottom for illustration.
This works fine except for one small problem: If a child partially sticks outside the parent container to the right, the text inside of it will wrap, trying to stay inside of the parent container. I do explicitly not want this - if a child moves out of the parent, it should not change its wrapping or size because of it, it should just move out of view. The parent should simply act as a "window" of sorts through which the children are viewed.
I've tried different combinations of applying white-space: nowrap
to the parent and/or children, none of which have worked for me so far. Is this just not possible?
This snippet demonstrates the problem:
/* Relevant parent and child styles */
.parent {
position: relative;
width: 500px;
height: 400px;
overflow: hidden;
}
.child {
position: absolute;
max-width: 250px;
}
/* Individual positioning of child elements */
#fine {
top: 30px;
left: 100px;
}
#wrapped {
top: 90px;
left: 400px;
}
#unwrapped {
top: 330px;
left: 170px;
white-space: nowrap;
}
/* The rest is only styles to make the example easier on the eye */
body {
background-color: hsl(0, 0%, 90%);
margin: 0;
padding: 24px;
}
.parent {
background-color: white;
border: 2px solid grey;
}
.child {
padding: 12px;
background: yellowgreen;
}
<div class="parent">
<div id="fine" class="child">
Text should be wrapped normally with the max-width of the child, like this
</div>
<div id="wrapped" class="child">
This text will be wrapped much earlier though because it is running out of the parent container
</div>
<div id="unwrapped" class="child">
"white-space: nowrap" only prevents *all* wrapping
</div>
</div>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…