I’m trying to create a two-column tabular layout with CSS flexbox. I want to have have column 1 to be as narrow as possible, tightly fitting all 3 cells in that column. I want column 2 to expand to the remaining width.
I was able to create this: https://codepen.io/ptspts/pen/KKErzeL. The problem with this is that each cell in column 1 has a different width. I want all of them to become as wide as the widest, automatically.
DIV.CONV {
display: flex;
flex-flow: column nowrap;
background-color: #888;
margin: 0;
padding: 0;
border: none;
}
DIV.CONV > DIV.ROW {
display: flex;
flex-flow: row nowrap;
width: 100%;
background-color: #ff0;
margin-top: 6px;
margin-bottom: 6px;
}
DIV.CONV > DIV.ROW > DIV.C1 {
display: inline;
flex: 1;
background-color: #800;
align-self: start; /* Vertical alignment. */
flex-grow: 0;
margin-right: 6px;
}
DIV.CONV > DIV.ROW > DIV.C2 {
display: block;
flex: 2;
background-color: #080;
align-self: stretch; /* Vertical alignment. Default. */
}
<DIV CLASS=CONV>
<DIV CLASS=ROW>
<DIV CLASS=C1>HI</DIV>
<DIV CLASS=C2>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</DIV>
</DIV>
<DIV CLASS=ROW>
<DIV CLASS=C1>FOOBAR</DIV>
<DIV CLASS=C2>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</DIV>
</DIV>
<DIV CLASS=ROW>
<DIV CLASS=C1>LAST</DIV>
<DIV CLASS=C2>Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</DIV>
</DIV>
</DIV>
Is this possible at all with CSS flexbox (if yes, how)? If not, is it possible with table, without JavaScript (if yes, how)? If not, is it possible with CSS grid (if yes, how)?
2
Answers
This would be better suited for CSS Grid.
Simply remove all the
rows
, and use a single withdisplay: grid
and set the template columns tomax-content 1fr
. Essentially the first column will be as wide as the content and the second will be all other remaining space.This is clearly a table layout