我有一个HTML表,我想将第一列的宽度设置为该列单元格中最大项目的宽度。
当我将宽度设置为50px时,看起来不错:

但是,如果我将宽度设置为“自动”,则会将其扩展得太多:

为什么这样做,我该如何解决?
这是我的代码:
<html>
<body>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;}
.tg td{font-family:Calibri, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;}
.tg th{font-family:Calibri, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;}
.tg .tg-j4kc{background-color:#efefef;text-align:center}
.tg .tg-ud5c{background-color:#efefef;text-align:right;width:auto}
p.italic{font-style: italic}
</style>
<font face="Calibri, Arial, Sans-Serif" font size="3">
The script<br><br>
<p class="italic">' + $ScriptName + '</p>
has sent a notification. See below for details...
</font><br><br>
<table class="tg" style='width:700px'>
<tr>
<th class="tg-j4kc" colspan="2">Script</th>
</tr>
<tr>
<td class="tg-ud5c">Path</td>
<td>"' + $ScriptPath + '"</td>
</tr>
<tr>
<td class="tg-ud5c">Server</td>
<td>' + $Server + '</td>
</tr>
<tr>
<td class="tg-ud5c">Name</td>
<td>' + $ScriptName + '</td>
</tr>
</table>
<font face="Calibri, Arial, Sans-Serif" font size="3">
<br><u>Notification</u>
<br><br>' + $NotificationContent + '
</font>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
编辑:
当我style='width:700px'从<table>参数中删除时,它似乎起作用。
我想为表格设置一个固定的宽度,因为可能会出现多个表格,而且我不希望每个表格都有不同的宽度。
小智 5
改变你的CSS如下:
tg .tg-ud5c {
background-color:#efefef;
text-align:right;
width:1%;
white-space: nowrap;
}
Run Code Online (Sandbox Code Playgroud)
更新的小提琴:http : //jsfiddle.net/Lk6bf5mm/1/
| 归档时间: |
|
| 查看次数: |
4524 次 |
| 最近记录: |