how to add a max width for td and th?

Hi everyone, I have a table like the image, I want to add max-width to label column, but it does not work. Is there any solution to solve this problem?
No description
18 Replies
noob
noobOP3mo ago
I don't want to set a fixed width for the <td>. I want to define a range of widths, like 200 to 400px, but only the min-width works properly
Chris Bolson
Chris Bolson3mo ago
I don't think that tables can have max-widths defined on the cells 🤔
noob
noobOP3mo ago
I couldn’t find a solution, so I switched to using CSS Grid. However, I ran into another problem. My row layout is defined as grid-template-columns: 60px minmax(240px, 320px) repeat(auto-fit, minmax(240px, 1fr));. I want the columns to overflow horizontally instead of wrapping onto a new line. How can I achieve this?
ἔρως
ἔρως3mo ago
dont use auto-fit if you dont know how many columns you will have, use grid-auto-columns and set the value to whatever you want the new columns to be like
noob
noobOP3mo ago
Sorry, I don’t understand. From what I know, grid-auto-columns is used to set the width for every column. However, in my case, the first column needs a fixed width, the second column should have a range of widths, and the rest should expand as needed. How can I achieve this with grid-auto-columns?
ἔρως
ἔρως3mo ago
it's actually meant for all the new columns you create outside of grid-template-columns also, you need to set grid-auto-flow: column https://jsfiddle.net/yqe8nhoj/ like this
Chris Bolson
Chris Bolson3mo ago
If you want the rest of the columns to expand as needed you don't even need to define the grid-auto-columns. You just define your first 2 template columns and let grid work out the rest, however, as Epic states, the grid-auto-flow: column; is needed.
grid-template-columns: 60px minmax(240px, 320px);
grid-auto-flow: column;
grid-template-columns: 60px minmax(240px, 320px);
grid-auto-flow: column;
ἔρως
ἔρως3mo ago
but that will set the columns to auto which is not what's in the post
Chris Bolson
Chris Bolson3mo ago
a couple of messages ago noob said this:
However, in my case, the first column needs a fixed width, the second column should have a range of widths, and the rest should expand as needed.
I understand "expand as needed" to have any added columns to have flexible widths (ie based on content). Anyway, there is enough info there to be able to achieve what they need in either case.
ἔρως
ἔρως3mo ago
but if you see the grid declaration, it has auto-fit which is meant to fit as many columns as possible
noob
noobOP3mo ago
grid-auto-flow and grid-auto-columns work well in my case. I want to add a min-width to the new columns, so I think I need to use grid-auto-columns for that. Thanks, guys!
ἔρως
ἔρως3mo ago
you're welcome
noob
noobOP3mo ago
Hi, I wonder if it is possible to make cells in the same column but different rows grow to the same size. For example, if the third cell in the first row expands to 300px because the content is too large, then the third cells in the other rows should also expand to 300px. Here is my current table structure. Should I use table element instead of grid layout?
<style>
ul{
overflow-x: auto;
}
.row {
display: grid;
grid-template-columns: 50px minmax(250px, 320px);
grid-auto-flow: column;
grid-auto-columns: minmax(max-content,1fr);
}
span{
border: solid 1px black;
}
</style>

<ul>
<li class="row">
<span>1</span>
<span>Lorem ipsum dolor sit.</span>
<span>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad vero voluptate ipsum suscipit l</span>
</li>
<li class="row">
<span>2</span>
<span>Label</span>
<span>Description</span>
</li>
</ul>
<style>
ul{
overflow-x: auto;
}
.row {
display: grid;
grid-template-columns: 50px minmax(250px, 320px);
grid-auto-flow: column;
grid-auto-columns: minmax(max-content,1fr);
}
span{
border: solid 1px black;
}
</style>

<ul>
<li class="row">
<span>1</span>
<span>Lorem ipsum dolor sit.</span>
<span>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad vero voluptate ipsum suscipit l</span>
</li>
<li class="row">
<span>2</span>
<span>Label</span>
<span>Description</span>
</li>
</ul>
ἔρως
ἔρως3mo ago
maybe something with subgrid should do the trick?
MarkBoots
MarkBoots3mo ago
ul{
overflow-x: auto;
display: grid;
grid-template-columns: max-content auto 1fr;
}
.row {
grid-column: 1/-1;
display: grid;
grid-template-columns: subgrid;
}
span{
border: solid 1px black;
}
ul{
overflow-x: auto;
display: grid;
grid-template-columns: max-content auto 1fr;
}
.row {
grid-column: 1/-1;
display: grid;
grid-template-columns: subgrid;
}
span{
border: solid 1px black;
}
noob
noobOP3mo ago
It seems to work, but in my case, users can add a new column to rows. I tried this, but it didn't work.
ul{
overflow-x: auto;
display: grid;
grid-template-columns: 50px minmax(250px, 320px);
grid-auto-flow: column;
grid-auto-columns: minmax(max-content,1fr);
}
.row {
grid-column: 1/-1;
display: grid;
grid-template-columns: subgrid;
}
span{
border: solid 1px black;
}
ul{
overflow-x: auto;
display: grid;
grid-template-columns: 50px minmax(250px, 320px);
grid-auto-flow: column;
grid-auto-columns: minmax(max-content,1fr);
}
.row {
grid-column: 1/-1;
display: grid;
grid-template-columns: subgrid;
}
span{
border: solid 1px black;
}
Chris Bolson
Chris Bolson3mo ago
The issue you are having is due to the way that the subgrid is spanning the columns in the parent grid. In Marks code he has defined grid-column: 1/-1; This means that the element will span all the columns from the first to the last declared (explicit) column. However, as you are allowing your users to add more columns this will no longer work as any columns added are no "implicit". How are they adding more columns? If it is via JS you could also use JS to modify the grid-colum declaration to specifically span the number of columns that you currently have defined, eg grid-column: 1/4; or grid-column: 1/5; ie adding (or removing) 1 as the columns are added.
noob
noobOP3mo ago
I see. It seems to work well when I declare end column explicitly. Thank you again

Did you find this page helpful?