表布局:固定忽略 td 的最小宽度

发布于 2024-11-28 17:53:17 字数 527 浏览 8 评论 0原文

我有一个项目需要一个包含固定宽度和灵活宽度列的表格。

无论浏览器的宽度是多少,某些列的宽度始终需要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写给空气的情书 2024-12-05 17:53:17

我的解决方法是将表格挤压到列几乎重叠的位置,检查该点的表格宽度,并将 元素本身的最小宽度设置为该值。如果您有一个可扩展列的固定布局,则此技术也很有用,否则扩展列将随着窗口缩小而完全消失。

例如: 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/

酒绊 2024-12-05 17:53:17

对于固定宽度的列,您需要使用 width,而不是 min-width,并且您应该将剩余列的宽度设置为基于列数的百分比。在您的例子中,您有 6 列,因此我将其设置为 16.66%

工作示例: http://jsfiddle.net/6vw66/

You need to be using width, not min-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 to 16.66%.

Working example: http://jsfiddle.net/6vw66/

护你周全 2024-12-05 17:53:17

我在其他资源上找到的解决方案:

  1. 在流体列之后添加额外的列到第一行
  2. ,在标题(固定部分)设置流体列的 width 样式
  3. 设置属性 colspan="2 “ 位于正文中的流体列上(将使用第一行中的额外列来自动调整空间)

之前(不起作用):

<table>
  <tr>
    <th style="min-width: 100px">col-1</th>
    <th style="width: 200px">col-2</th>
  </tr>
  <tr>
    <td>col-1 content</td>
    <td>col-2 content</td>
  </tr>
</table>

之后:

<table>
  <tr>
    <th style="width: 100px">col-1</th> <!-- 2. -->
    <th><!--special column--></th>      <!-- 1. -->
    <th style="width: 200px">col-2</th>
  </tr>
  <tr>
    <td colspan="2">col-1 content</td>  <!-- 3. -->
    <td>col-2 content</td>
  </tr>
</table>

Solution I've found on the other resource:

  1. add extra column just after fluid column to the first row
  2. set width style of fluid column in the header (fixed part)
  3. set attribute colspan="2" on fluid column in the body (will use extra column in the first row to auto adjust space)

Before (not working):

<table>
  <tr>
    <th style="min-width: 100px">col-1</th>
    <th style="width: 200px">col-2</th>
  </tr>
  <tr>
    <td>col-1 content</td>
    <td>col-2 content</td>
  </tr>
</table>

After:

<table>
  <tr>
    <th style="width: 100px">col-1</th> <!-- 2. -->
    <th><!--special column--></th>      <!-- 1. -->
    <th style="width: 200px">col-2</th>
  </tr>
  <tr>
    <td colspan="2">col-1 content</td>  <!-- 3. -->
    <td>col-2 content</td>
  </tr>
</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文