简体   繁体   English

固定列的高度和宽度

[英]fixed column height and width

I am having a table as follows: 我有一张桌子,如下所示:

<table>
<tr style ="height: 10px;" >
<td style="width: 200px, height : "10px;"> </td> <td style="width: 200px , height : "10px;"> </td> <td style="width: 200px , height : "10px;"> </td> <td style="width: 200px , height : "10px;"> </td>    

</tr>
</table>

The problem is, when the contents in the second column of any row are slightly large, the width of the second column exceeds 150px, and to compensate the width of the first column reduces. 问题是,当任何一行的第二列中的内容都稍大时,第二列的宽度超过150px,并且为了补偿第一列的宽度而减小。 How can I prevent that from happening. 我如何防止这种情况发生。 I want to widths to not change and even if the extra texts are not shown it`s fine. 我希望宽度保持不变,即使没有显示多余的文字也没关系。

I also want the height of the rows and columns to be of 3 lines of text and fixed in height. 我还希望行和列的高度为3行文本,并固定高度。

First off, the code was incorrect. 首先,代码不正确。 Here's your code corrected, try does it work what you wanted it to: 这是您的代码已更正,请尝试按您希望的方式工作:

<table>
<tr style="height: 10px;" >
<td style="width: 200px; height:10px;"></td>
<td style="width: 200px; height:10px;"></td>    
<td style="width: 200px; height:10px;"></td>
<td style="width: 200px; height:10px;"></td>
</tr>
</table>

Second, the way you're styling is very old-school and hard on you, try creating a CSS class which you can then apply to every element, no need to repeat the rules. 其次,您的样式设置很老套并且很难,请尝试创建CSS类,然后将其应用于每个元素,而无需重复规则。 In fact, if this will be the only table on your page, you can put something like this inside head: 实际上,如果这将是页面上的唯一表,则可以将以下内容放在头部:

<style type="text/css">
td {
width: 200px;
height:10px;
}
</style>

That will apply your rules to all tags on page, so you don't have to explicitly style each and every one. 这会将您的规则应用于页面上的所有标签,因此您不必显式地设置每个标签的样式。

Or you can do: 或者,您可以执行以下操作:

<style type="text/css">
.exampleclass {
width: 200px;
height:10px;
}
</style>


<table>
<tr style="height: 10px;" >
<td class="exampleclass"></td>
<td class="exampleclass"></td>    
<td class="exampleclass"></td>
<td class="exampleclass"></td>
</tr>
</table>

That way you control your styling from one place, and are also able to apply it to other elements as you see fit. 这样一来,您可以从一个地方控制样式,也可以根据需要将其应用于其他元素。

If there's anything else, ask away. 如果还有其他问题,请走开。

EDIT: And for fulfilling your requirement of widths being fixed at cost of extra content not showing, apply both answers of Guzzie and QQping. 编辑:为了满足您对宽度固定的要求,但不显示额外的内容,请同时应用Guzzie和QQping的答案。 Although if you're ok with varying height, you don't have to set overflow:hidden; 尽管如果您对不同的高度没问题,则不必设置overflow:hidden;

只需将max-width添加到表单元格即可。

You should set the table's style to fixed like this and add the total width of the table 您应该将表格的样式设置为固定,并添加表格的总宽度

<table style='table-layout:fixed' width='300px'>

Firefox may not like to see table cells with overflowing long texts cause of fixed column-widths, to better display this you should set the following TD style in your css or on your current page Firefox可能不希望看到表格单元格长文本溢出导致固定列宽的原因,为了更好地显示此信息,您应该在CSS或当前页面中设置以下TD样式

<style>
 td {overflow:hidden;}
</style>

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

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