admin管理员组文章数量:1431927
I'm building a website in React, using Material-UI. I have a table and was wondering, if it's possible to set column width to fit the data + some additional padding on both ends?
This is my table:
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<TableCell width="???">CA</TableCell> <--- Width should be as much as "Fit to data" + margin of X pixels
<TableCell width="140px">CB</TableCell>
<TableCell width="240px">CC</TableCell>
<TableCell width="200px">CD</TableCell>
<TableCell>CE</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map((row) => (
<TableRow key={row.id}>
<TableCell>{row.ca}</TableCell>
<TableCell>{row.cb}</TableCell>
<TableCell>{row}</TableCell>
<TableCell>{row.cd}</TableCell>
<TableCell>{row.ce}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
I'd like column CA
to be minimal, just so it fits the data with additional space on both sides, so that content doesn't touch column borders.
I've tried:
- not declaring
width
at all - didn't work width="auto"
- didn't work
I'm building a website in React, using Material-UI. I have a table and was wondering, if it's possible to set column width to fit the data + some additional padding on both ends?
This is my table:
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<TableCell width="???">CA</TableCell> <--- Width should be as much as "Fit to data" + margin of X pixels
<TableCell width="140px">CB</TableCell>
<TableCell width="240px">CC</TableCell>
<TableCell width="200px">CD</TableCell>
<TableCell>CE</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map((row) => (
<TableRow key={row.id}>
<TableCell>{row.ca}</TableCell>
<TableCell>{row.cb}</TableCell>
<TableCell>{row}</TableCell>
<TableCell>{row.cd}</TableCell>
<TableCell>{row.ce}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
I'd like column CA
to be minimal, just so it fits the data with additional space on both sides, so that content doesn't touch column borders.
I've tried:
- not declaring
width
at all - didn't work width="auto"
- didn't work
1 Answer
Reset to default 3You can target this th
with css and set it to width: 1px; white-space: nowrap;
I think this should work in your case:
.MuiTableCell-head:first-child {
width: 1px;
white-space: nowrap;
}
And here's how this can be achieved using react inline-styling:
// You can also add padding or any other style props to this style object
<TableCell style={{width: '1px', whiteSpace: 'nowrap'}}>CA</TableCell>
本文标签: javascriptHow to set smallest width on table column in MaterialUIStack Overflow
版权声明:本文标题:javascript - How to set smallest width on table column in Material-UI? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745591133a2665203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论