表布局:固定忽略 td 的最小宽度
我有一个项目需要一个包含固定宽度和灵活宽度列的表格。
无论浏览器的宽度是多少,某些列的宽度始终需要为 80px。
其余列需要具有灵活的宽度,但全部具有相同的宽度,最小宽度为 75px;
通过添加 table-layout:fixed;在表格中,我能够获得灵活宽度的列,使其全部具有相同的宽度,但是然后表格会忽略 td 的 min-width 属性。
如果我删除 table-layout:fixed,则最小宽度有效,但灵活宽度列都有不同的宽度(基于它们包含的文本的长度)。
列数根据从数据库中提取的数据而变化。
我在 jsfiddle 上放置了带有 table-layout:fixed 和 min-width 的表格示例: http://jsfiddle.net /7d2Uj/
有什么想法如何让最小宽度在这种情况下工作,或者为什么会发生这种情况?
I have a project which requires a table that includes both fixed-width and flexible-width columns.
Some of the columns need to be 80px in width always, no matter what the width of the browser.
The rest of the columns need to be a flexible width, but all be of equal width, with a min-width of 75px;
By adding table-layout:fixed; to the table I am able to get the flexible width cols to all have equal widths, BUT then the table ignores the td's min-width attribute.
If I remove the table-layout:fixed, the min-width works but the flexible width cols all have different widths (based upon the length of the text they contain).
The number of columns vary based upon data pulled from db.
I have put an example of the table with table-layout:fixed and min-width on jsfiddle: http://jsfiddle.net/7d2Uj/
Any ideas how to get min-width to work in this situation, or why this is occurring?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的解决方法是将表格挤压到列几乎重叠的位置,检查该点的表格宽度,并将
元素本身的最小宽度设置为该值。如果您有一个可扩展列的固定布局,则此技术也很有用,否则扩展列将随着窗口缩小而完全消失。
例如: http://jsfiddle.net/KTCfs/
My workaround for this was to squeeze the table to where the columns almost overlap, check the table width at that point, and set the min-width of the
<table>
element itself to that value. This technique is also useful if you have fixed layout with one expandable column because otherwise the expanding column would completely disappear as the window shrinks.Ex: http://jsfiddle.net/KTCfs/
对于固定宽度的列,您需要使用
width
,而不是min-width
,并且您应该将剩余列的宽度设置为基于列数的百分比。在您的例子中,您有 6 列,因此我将其设置为16.66%
。工作示例: http://jsfiddle.net/6vw66/
You need to be using
width
, notmin-width
for the fixed width columns, and you should set the width of the remaining columns to a percentage based on the number of columns. In your case, you had 6 columns, so I set it to16.66%
.Working example: http://jsfiddle.net/6vw66/
我在其他资源上找到的解决方案:
width
样式colspan="2 “
位于正文中的流体列上(将使用第一行中的额外列来自动调整空间)之前(不起作用):
之后:
Solution I've found on the other resource:
width
style of fluid column in the header (fixed part)colspan="2"
on fluid column in the body (will use extra column in the first row to auto adjust space)Before (not working):
After: