使 <td> 的宽度适合其内容
fit width of <td> to its content
我有一个 HTML table,我想将第一列的宽度设置为该列单元格中最大项目的宽度。
当我将宽度设置为 50px 时,它看起来不错:
但是,如果我将宽度设置为 'auto',它会扩展得太多:
为什么会这样,我该如何解决?
这是我的代码:
<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>
编辑:
当我从 <table>
参数中删除 style='width:700px'
时,它似乎起作用了。
我希望 table 有一个固定的宽度,因为可能会出现多个 table 并且我不希望每个 table ] 具有不同的宽度。
如下更改您的 css :-
tg .tg-ud5c {
background-color:#efefef;
text-align:right;
width:1%;
white-space: nowrap;
}
已更新 fiddle:http://jsfiddle.net/Lk6bf5mm/1/
根据这是否旨在作为响应式 table,您可以考虑为您的列使用 max-width 或 min-width 属性,作为先前建议的另一种选择。
我有一个 HTML table,我想将第一列的宽度设置为该列单元格中最大项目的宽度。
当我将宽度设置为 50px 时,它看起来不错:
但是,如果我将宽度设置为 'auto',它会扩展得太多:
为什么会这样,我该如何解决?
这是我的代码:
<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>
编辑:
当我从 <table>
参数中删除 style='width:700px'
时,它似乎起作用了。
我希望 table 有一个固定的宽度,因为可能会出现多个 table 并且我不希望每个 table ] 具有不同的宽度。
如下更改您的 css :-
tg .tg-ud5c {
background-color:#efefef;
text-align:right;
width:1%;
white-space: nowrap;
}
已更新 fiddle:http://jsfiddle.net/Lk6bf5mm/1/
根据这是否旨在作为响应式 table,您可以考虑为您的列使用 max-width 或 min-width 属性,作为先前建议的另一种选择。