简体   繁体   English

具有可变,固定和剩余宽度的HTML表

[英]HTML table with variable, fixed, and remaining width

I need to create an HTML table with the following layout: 我需要创建一个具有以下布局的HTML table

[Name] [Message] Date] [Name] [Message] Date]

Where the width of [Name] should be the width of the longest name (Up to a max), [Date] should be a fixed width of 95px (And floating to the right), while [Message] should take the remaining width. 如果[Name]的宽度应该是最长名称的宽度(最大为最大值), [Date]应该是95px的固定宽度(并向右浮动),而[Message]应该取剩余的宽度。

I've tried using multiple div's , but I can't get the result I need, and a table seems much simpler. 我尝试过使用多个div's ,但是我无法得到我需要的结果,而且table似乎更简单。

So far, the following isn't working: 到目前为止,以下不起作用:

<table style="width: 100%">
<tr>
<td style="width: 100%; max-width: 100px">NAME</td>
<td style="width: 100%">message</td>
<td style="width: 95px">TIME</td>
</tr>
<tr>
<td style="width: 100%; max-width: 100px">NAME OTHER</td>
<td style="width: 100%">message</td>
<td style="width: 95px">TIME</td>
</tr>
</table>

Edit 1 Seems as though this example has exactly what I need. 编辑1似乎这个例子正是我需要的。 Although I still think a table would be neater. 虽然我仍然认为table会更整洁。
Edit 2 The [Message] needs to allow for multiline... 编辑2 [Message]需要允许多行...
Edit 3 Here is a working sample of what I need (Exactly) based on the link in Edit 1 编辑3 这是基于编辑1中的链接我所需要的(完全) 的工作示例

This cannot be done in CSS alone, due to the requirements. 由于要求,这不能仅在CSS中完成。 The first column should be flexible, which is easy (just prevent line breaks and let the column take its natural width), and setting the last column width is trivial, but telling the browser to use all the rest in the mid column (instead of expanding the first column too) cannot be done in CSS. 第一列应该是灵活的,这很容易(只是防止换行并让列采用其自然宽度),并设置最后一列宽度是微不足道的,但告诉浏览器使用中间列中的所有其余部分(而不是扩展第一列也无法在CSS中完成。 If you set its width to 100%, things work the desired way in some browsers, but other browsers (like IE) treat it differently. 如果将其宽度设置为100%,则在某些浏览器中可以使用所需的方式,但其他浏览器(如IE)会以不同的方式对待它。 You would require a width of something plus 100% plus 95px to equal 100%, which is of course impossible, and browsers handle this in different ways. 你需要宽度加上100%加95px等于100%,这当然是不可能的,浏览器以不同的方式处理这个问题。

However, with a little bit of JavaScript the medicine goes down: do as outlined above, with 100%, then postprocess the table by setting the first column to a specific width in pixels (using the value allocated by the browser), remove the width: 100% setting, and set table layout to fixed—which means that the browser now has two columns width fixed width, total width set to 100%, and one column with no width set, so it is easy to it to allocate the remaining width to the mid column. 然而,使用一点点JavaScript药物就会失效:如上所述,100%,然后通过将第一列设置为特定宽度(以像素为单位)(使用浏览器分配的值)对表进行后处理,删除width: 100%设置,并将表格布局设置为固定 - 这意味着浏览器现在有两列宽度固定宽度,总宽度设置为100%,一列没有设置宽度,因此很容易分配剩余的宽度到中间列。

<style>
td:first-child  { white-space: nowrap }
td:nth-child(2) { width: 100% }
td:nth-child(3) { width: 95px }
</style>
<table border cellspacing=0 style="width: 100%">
<tr>
<td style="">NAME</td>
<td style="">message</td>
<td style="width:95px">TIME</td>
</tr>
<tr>
<td>NAME OTHER</td>
<td>message</td>
<td>TIME</td>
</tr>
</table>
<script>
(function () {
var row = document.getElementsByTagName('tr')[0];
var cell1 = row.children[0];
cell1.style.width = cell1.clientWidth + 'px';
row.children[1].style.width = 'auto';
document.getElementsByTagName('table')[0].style.tableLayout = 'fixed';
})();
</script>

For simplicity, this code is based on the assumption that there are no other tables on the page. 为简单起见,此代码基于页面上没有其他表的假设。 Modify as needed. 根据需要修改。 The attributes border cellspacing=0 are there just make the cell widths more visible. border cellspacing=0的属性只是使单元格宽度更加明显。

Update: This does not address the issue of setting a maximum width on the first column. 更新:这不解决在第一列上设置最大宽度的问题。 That requirement is underdefined unless you specify what should happen if the width is exceeded (truncation, word wrap, wrap anywhere, wrap with hyphenation?). 除非您指定超出宽度时应该发生什么(截断,自动换行,随处换行,用连字换行?),否则该要求未定义。

try this code . 试试这段代码。

.test
{
max-width:100px;
} 


<table style="text-align: center;">
<tr>
<th>NAME</th>
<th>message</th>
<th style="width: 95px">TIME</th>
</tr>
<tr>
<td class="test">NAME OTHER</td>
<td>message</td>
<td style="width: 95px">TIME</td>
</tr>
</table>

The following .css code provides the template for the attached picture: 以下.css代码提供了附加图片的模板:

    table {
        display: table;
        width: 100%;
        table-layout: fixed;
        position: absolute;
        top: 10px;
        empty-cells: hide;
    }

    td.small:first-Child {
        vertical-align: top;
        width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    td.small:last-Child {
        vertical-align: top;
        width: 95px;
    }

    td.extend {
        vertical-align: top;
        word-wrap: break-word;
    }

    .userName a {
        color: #9DC8FC;
    }

<tr>
   <td class="small userName">
         <a title="Administrator" href="#">Administrator</a>
   </td>
   <td class="extend">
         is it me you're looking for?
   </td>
   <td class="small">
         10:14:01 AM
   </td>
</tr>

客舱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM