html 列宽调整 (td width 不生效问题)

解决HTML表格中td width属性不生效的问题,需设置table-layout:fixed。然而,单元格内容默认不自动换行,仅对单词间空格换行,中文可以正常换行。当表格仅有两列且width设为25%时,宽度设置无效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:给 table 加上 table-layout:fixed 属性

2: 给 td 加上 width属性

<html>
<body>

<style>

.mytable {
table-layout:fixed
}

</style>

<table class = "mytable" border="1"  >
  <tr>
    <th width="300px">Month</th>
  </tr>
  <tr>
    <td width="300px">Januarydffffffffdaf</td>
  </tr>
</table>

</body>
</html>

 

但是比如,我们把width 改为 10,可以看到表格宽度不再减少:

也就是说,默认长度至少为单元格内容长度(不换行)。

 怎么自动换行呢?

自动换行

.mytable {
word-wrap: break-word;
word-break: break-all;
table-layout:fixed;
}

为什么不生效???

 摸索了许久,这里只能对单词间的空格进行换行,坑,大坑。

不过中文 是OK的:

另一个问题

表格默认会撑满整个table的宽度,

<html>
<body>

<style>
.mytable {
position: absolute;
left: 14%;
width:72%;
word-wrap: break-word;
word-break: break-all;
table-layout:fixed;
}
</style>
<table class = "mytable" border="1"  >
  <tr>
    <td width="25%" >哒哒哒哒</td>
    <td width="25%" >哒哒哒哒</td>
    <td >哒哒哒哒</td>
    <td >哒哒哒哒</td>
    <td >哒哒哒哒</td>
    <td >哒哒哒哒</td>
  </tr>
</table>
</body>
</html>

<table class = "mytable" border="1"  >

  <tr>
    <td width="25%" >哒哒哒哒</td>
    <td width="25%" >哒哒哒哒</td>
  </tr>
</table>

 如果只有两例的时候, width = 25% 不生效。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zonson9999

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值