I have a full-width component which is made of three parts: a central one which width is driven by its (fixed) content (a string) and two panes left and right from that central element which should equally take the remaining width.
One way to do that is to use a grid
display and set the columns as grid-template-columns: 1fr auto 1fr;
:
#container {
display: grid;
grid-template-columns: 1fr auto 1fr;
}
<div id="container">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ultricies at quam vel efficitur. Donec dictum lorem eu dolor pulvinar facilisis. Fusce mollis egestas orci et consectetur. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur
pulvinar, sem nec eleifend tincidunt, neque eros porttitor arcu, nec porttitor sem felis id risus. Nulla posuere, lectus vitae auctor rhoncus, metus purus pellentesque ante, vel interdum ex metus eget nisl. Nunc feugiat ipsum pharetra, ultricies justo
ac, condimentum risus.
</div>
<div>hello</div>
<div>world</div>
</div>
It works as expected: the central elements takes its space, and both panes (lorem ipsum and world) equally share the remaining one.
In my real application, I have the left and right pane filled in by elements, which are driven by display: flex
. It looks like this setup overrides the width of the parent element, which is supposed to be 1fr
but is extended as the elements fill it in, like in the example below:
#container {
width: 600px;
display: grid;
grid-template-columns: 1fr auto 1fr;
}
#lpane {
display: flex;
flex-direction: row;
}
<div id="container">
<div id="lpane">
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
<div>something</div>
</div>
<div>hello</div>
<div>world</div>
</div>
Why is it so? Is there a way to force the parent element to actually follow its setting of 1fr
and not expand as it is filled with flex
elements (which should then flow into a new line, constrained by the width of the parent)?