【问题标题】:HTML Table - 100% width table, combination of fixed width and UNIFORM fluid columnsHTML Table - 100% 宽度的表格,固定宽度和 UNIFORM 流体列的组合
【发布时间】:2012-07-15 11:29:30
【问题描述】:

我正在尝试用 HTML 和 Javascript 构建一个自定义日历,您可以在其中将任务从一天拖放到另一天。我希望将第一列和最后两列设为固定宽度,并让其余列(周一至周五)保持流动,以便表格始终填满其父级的 100%。

我遇到的问题是流体 TD 会根据其中的内容量自动调整大小,这意味着当我将一项任务从一天拖到另一天时,列宽会调整大小。无论内容如何,​​我都希望周一到周五的大小完全相同,并且不设置 text-overflow:hidden; (因为任务总是需要可见的)

即我希望灰色列固定宽度,红色列流动但彼此一致,无论其中的内容如何。

编辑:我正在使用 jQuery 来实现拖放功能,因此 JavaScript 解决方案也可以(虽然不是更可取)。

JSFiddle Live example

HTML:

<table>
  <tr>
    <th class="row_header">Row Header</th>
    <th class="fluid">Mon</th>
    <th class="fluid">Tue</th>
    <th class="fluid">Wed</th>
    <th class="fluid">Thurs</th>
    <th class="fluid">Fri</th>
    <th class="weekend">Sat</th>
    <th class="weekend">Sun</th>
  </tr>
  <tr>
    <td>Before Lunch</td>
    <td>This col will be wider than the others because it has lots of content...</td>
    <td></td>
    <td></td>
    <td></td>
    <td>But I would really like them to always be the same size!</td>
    <td></td>
    <td></td>
  </tr>
</table>

CSS:

    table {
        width: 100%;
    }       

    td, th {
        border:1px solid black;
    }  

    th {
        font-weight:bold;            
        background:red;
    }

    .row_header {
        width:50px;
        background:#ccc;
    }

    .weekend {
        width:100px;
        background:#ccc;
    }

    .fluid {
        ???
    }

【问题讨论】:

  • 非常好的问题。我不确定它可以用纯CSS来完成。您可能需要使用 JavaScript 来计算表格的总宽度并相应地应用样式。

标签: html css html-table fluid


【解决方案1】:

丹尼,

我想这就是你正在寻找的。​​p>

在这里提琴http://jsfiddle.net/T6YNK/22/

已在 Chrome 中检查。

代码

                <table>
      <tr>
        <th class="row_header">Row Header</th>
        <th class="fluid">Mon</th>
        <th class="fluid">Tue</th>
        <th class="fluid">Wed</th>
        <th class="fluid">Thurs</th>
        <th class="fluid">Fri</th>
        <th class="weekend">Sat</th>
        <th class="weekend">Sun</th>
      </tr>
      <tr>
        <td>Before Lunch</td>
        <td>This col will be wider than the others because it has lots of content...</td>
        <td></td>
        <td></td>
        <td></td>
        <td>But I would really like them to always be the same size!</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>

    <style type="text/css">

        table {
            width: 100%;
        }        

        td, th {
            border:1px solid black;
        }   

        th {
            font-weight:bold;            
            background:red;
        }

        .row_header {
            width:50px;
            background:#ccc;
        }

        .weekend {
            width:100px;
            background:#ccc;
        }

        td,
        th        {
            overflow:hidden;
        }
    .fluid {

}
.weekend,
.row_header{
width:50px !important;

}

table{
    border: 1px solid black;
    table-layout: fixed;
    width:100%;

}
    </style>​

【讨论】:

  • 最佳答案,仅使用 CSS,不使用 jQuery。
【解决方案2】:

使用 jQuery(可能也可以使用常规 JS,但我更喜欢这种工作):

(注意:我给了表格一个 ID,所以它会更容易选择,没有它也可以通过一些修补来完成)

    $(function() {
        var $my_table = $("#my_table")
            ,current_width = $my_table.width()
            ,fluid_columns = $("table .fluid")
            ,spread_width = (current_width - 150) / fluid_columns.length;

        fluid_columns.width(spread_width);

        $(window).on("resize", function() {
            current_width = $my_table.width();
            spread_width = (current_width - 150) / fluid_columns.length;
            fluid_columns.width(spread_width);
        })
    });

【讨论】:

  • 是的,这让我们非常接近。不幸的是,在非常小的分辨率下它仍然会崩溃。也许我需要意识到浏览器正在试图帮助我并在表格部分放置一个中间宽度以避免极端示例。
【解决方案3】:

你能做到吗:

.fluid {
    width:10%;
} 

【讨论】:

  • 这似乎对我不起作用。 .row_header 和 .weekend 上的宽度似乎被忽略了,在小分辨率上它仍然不均匀地调整列的宽度Sample Here
  • 你能做媒体查询,然后适当地改变百分比吗?
  • 这有什么帮助?可以举个例子吗?
【解决方案4】:

仅使用 CSS 怎么样?

http://jsfiddle.net/T6YNK/16/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 2014-11-14
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2012-12-29
    • 2014-02-10
    • 1970-01-01
    相关资源
    最近更新 更多