I want to display data in a table, but tag is not allowing me to do the things I want to. The next best thing is using grid, however I am unable to style columns in a way that suits me.
My goal is to make column width be the same width of the widest cell of that column. All columns would be dynamic width.
At the moment my css looks like this:
display: grid;
grid-template-columns: repeat(13, 1fr);
width: 100%;
border: 1px solid black;
And all the columns are the same width and not at all dynamic. If I change 1fr > auto; then each row cells have different widths.
In short, I want each rows columns to be the same width as the widest cell of that column from all rows.
Can I achieve this with just html and css or do i need to use js? Should I try doing this with flexbox instead? The is out of question due to it not fitting the needs of the app.
Edit to clarify expectations and result:
If I set it as repeat(13, 1fr);, then all columns in all rows will be same width. This is not the desired outcome, because I want some columns to be wider or thinner depending on the content inside.
If I set repeat(13, auto);, then columsn have dynamic width, however that happens to all rows, meaning in result I get different col width in each row (one col width might be larger than same col in a different row).
What I want to ultimately achieve, is make a table like structure, where col width on all rows is the width of the widest cell in that column of all the rows. If you take a look at how table tag does it.
Reason why I am not just using table: I need other elements inside the table, that are not allowed inside table tag.
2
Answers
I’m not perfectly sure I got what you meant, but if you intended to have a grid layout table-like where each cell extends its width as max as the widest cell on the same column, but yet not being any wider, a solution could be having the width of the container as
width: fit-content;
and the the grid layout asgrid-template-columns: repeat(13, auto);
You can, of course, do this with grid and for that your javascript needs to access relevant element(s) of the document appropriately. The code may be as under: