简体   繁体   English

如何使html表列(只有文本的列)均匀分布

[英]How can I make html table columns (only the ones with text) evenly spaced

I wondering if there is an easy way to do this via HTML / CSS. 我想知道是否有一种简单的方法可以通过HTML / CSS完成此操作。

I have a dynamic fixed-width table with 4 columns: 我有一个动态的固定宽度表,有4列:

<table>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>

What I want is always have all the columns with data have all the same width. 我想要的是所有具有数据的列都具有相同的宽度。

So if any column is completely empty, I want it to collapse and the remaining columns would all have the same width. 因此,如果任何列完全为空,我希望它折叠,其余列都具有相同的宽度。

I know that my jsFiddle is not correct, but I wanted to have one as a starting point. 我知道我的jsFiddle不正确,但我想以一个为起点。

I did review How to get table cells evenly spaced? 我做了回顾如何让表格细胞均匀分布? , but is not exactly what I am looking for. ,但不完全是我想要的。 I also reviewed Evenly spaced fixed-width columns - in a responsive setting , but that one also not the same. 我还审查了均匀间隔的固定宽度列 - 在响应设置中 ,但也不一样。

EDIT : 编辑

td:empty {display: none} is a not the solution because if another row has has text in that column, the table gets distorted. td:empty {display: none}不是解决方案,因为如果另一行在该列中有文本,则表会失真。 jsFiddle 的jsfiddle

UPDATE UPDATE

in case of more than one row : 如果是多行:

Solution is as describe below, to have it work, you need <tr> to be displayed as a single table : http://jsfiddle.net/zTbGB/7/ http://jsfiddle.net/zTbGB/8/ 解决方案如下所述,要使其工作,您需要<tr>显示为单个表: http//jsfiddle.net/zTbGB/7/ http://jsfiddle.net/zTbGB/8/

tr {
    display:table;
     table-layout: fixed;
    width:100%;
}

================================================= =================================================

you can set : table-layout:fixed on table element. 你可以设置: table-layout:fixed在table元素上。 http://jsfiddle.net/zTbGB/1/ http://jsfiddle.net/zTbGB/1/

See: http://www.w3.org/TR/CSS21/tables.html#fixed-table-layout 请参阅: http//www.w3.org/TR/CSS21/tables.html#fixed-table-layout

Then you can use the :empty selector : 然后你可以使用:empty选择器:

td:empty {display:none;}

http://jsfiddle.net/zTbGB/3/ http://jsfiddle.net/zTbGB/3/

You can try this Fiddle . 你可以尝试这个小提琴 Seems to work well. 似乎运作良好。 In the CSS below, I'm using the ::empty pseudo selector. 在下面的CSS中,我使用:: empty伪选择器。 That selector won't work in IE8 or less, so if you're supporting IE8, then you'll need to augment this code with some javascript to hide those empty <td> 's. 该选择器不能在IE8或更低版本中工作,所以如果你支持IE8,那么你需要用一些javascript来增加这些代码来隐藏那些空的<td>

CSS CSS

table
{
    width: 400px;
    table-layout: fixed;
}
td:empty { display: none; }

I have tried code for you using jQuery :) , you can use following code using jQuery: 我已经尝试使用jQuery代码:),您可以使用以下代码使用jQuery:

$("#parent td").each(function() {


     if ($(this).text() == ''){
         var val=$(this).val();
        //alert(len);
     $(this).remove();


     }

});
var len=$("#parent td").length;
         var wit=100/len;
     //wit=wit+"%";
$("table td").width(wit + "%");

updated here http://jsfiddle.net/zTbGB/6/ 在这里更新http://jsfiddle.net/zTbGB/6/

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

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