简体   繁体   English

固定html表中列的宽度

[英]Fixed width of columns in html table

I am trying to use a table where I will define fixed width columns. 我正在尝试使用一个表,在其中定义固定宽度的列。 Each column width is pre-defined and applied to a 'th' element in the table header via: ng-style='{"width":get_width[i]+"px"}', where the get_width function returns a number. 每个列的宽度都是预定义的,并通过ng-style ='{“ width”:get_width [i] +“ px”}'应用于表格标题中的第'th'个元素,其中get_width函数返回一个数字。

This works just fine as long as the text does not exceed the column width specified. 只要文本不超过指定的列宽,此方法就可以正常工作。 If it does, the width changes to accommodate the entire text. 如果是这样,则宽度会更改以容纳整个文本。

Question, how can I use CSS to force the column width to my specification? 问题,如何使用CSS强制列宽达到我的规范? The text may be truncated if longer. 如果更长,则文本可能会被截断。

Thx in advance. 提前谢谢。

You can truncate text using css: FIDDLE 您可以使用CSS截断文本: FIDDLE

CSS 的CSS

.div1 {
    white-space:nowrap;
    width:12em;
    overflow:hidden;
    text-overflow:ellipsis;
    border:1px solid #000000;
}

HTML 的HTML

<p>This div uses "text-overflow:ellipsis":</p>
<table>
    <tr>
        <td><div class="div1">This is some long text that will not fit in the box.</div></td>
    </tr>
</table>

If you can, set the width of the table as a whole by styling the <table> element. 如果可以,请通过设置<table>元素的样式来设置整个表格的宽度。 Evidently a lot of implementations of CSS expand the columns to fit text until they bump up against the maximum width of the table. 显然,许多CSS实现都会扩展列以适合文本,直到它们碰到表格的最大宽度为止。

Source 资源

you need to set table layout fixed and set overflow hidden for cells and you have to use 您需要设置表格布局固定并为单元格设置隐藏的溢出,并且必须使用

<table class="fixed-table">
    <col width="20px" />
    <col width="30px" />
    <col width="40px" />
    <tr>
        <td>text</td>
        <td>text</td>
        <td>text</td>
    </tr>
</table>

.fixed-table{ table-layout:fixed; word-break: break-all;}
.fixed-tabletd { overflow: hidden; }

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

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