Instead of attempting to determine the maximum between 100% and adjusting to fit the content, consider setting the minimum width of your <div>
to match its parent's size. By setting the container in question (represented by the blue box below) as an inline-block
with a minimum width of 100% relative to its parent (the red box), I was able to achieve the desired outcome.
This approach ensures that the container always occupies 100% of its parent's width unless the parent is resized to be smaller than the inner content. In such cases, the container will not shrink beyond the content's width, necessitating scrolling within the parent (red box).
This technique is effective when using the display properties flex
and inline-block
, but not with block
. Perhaps someone here can offer insight into why this limitation exists.
For demonstration purposes, I have created an example where the parent (red box) can be resized to observe the solution in action:
.outer {
width: 500px;
resize: horizontal;
overflow: auto;
border: 5px solid red;
}
.inner {
display: inline-block; /* or flex */
min-width: 100%;
border: 5px solid blue;
box-sizing: border-box;
}
.content {
white-space: nowrap;
background-color: lightGrey;
}
<div class="outer">
<div class="inner">
<div class="content">For the blue box, the maximum aligns with the red box and the minimum aligns with the content</div>
</div>
</div>