I have a centered div (using absolute positioning and transform:translate(-50%,-50%) as described here). When the content of the child div is short everything works fine. When the content is long, then the child div width is limited to 50% of the container div (probably because left is set to 50%).
Is there a way to allow the child div to expand to the full width only when the content is long?
.container {
position: relative;
display: inline-block;
width: 200px;
height: 200px;
margin-left: auto;
margin-right: auto;
border: 2px solid black;
}
.child {
position: absolute;
transform: translate(-50%, -50%);
left:50%;
top:50%;
background:lightgreen;
}
<div class="container">
<div class="child">short</div>
</div>
<div class="container">
<div class="child">long long long long long long long</div>
</div>
Edit: I found an explanation that helped me wrap my head around this in this answer.