html - Border-Box 100% 宽度计算 - 文本不适合

标签 html css forms button width

我有一个有趣的盒子模型/宽度/继承问题,我正在困惑。基本上,这就是我所拥有的:

<table>
  <tr>
    <td>
      <div><input type="text">So the td is at least this big</input></div>
      <div>
        <button>The Fish Molecular Genetics and Biotechnology Laboratory</button>
        <button>Each button must be as wide as the widest one</button>
      </div>
    </td>
  </tr>
<table>

在这个简化模型中,<td>有(而且必须有)自动宽度,<button>具有 100% 宽度和 display:block;这样所有按钮的宽度都相同,<button>也有一些填充,几乎所有东西都有 box-sizing:border-box; .

因此,当一个按钮被渲染时,它的框宽度被设置为它包含的文本的宽度。但是,等等,它的框宽度是从一个边界到另一个边界测量的,并且它有一个填充,所以文本实际上换行到下一行。而且它必须是边界框,因为上面简化模型中不明显的许多充分理由。 td 必须具有自动宽度,因为按钮会根据文本框中的内容实时更改。

最佳答案

这是一个不错的修复,但我仍然认为这是一个错误。

<div>
  <button><span>The Fish Molecular Genetics and Biotechnology Laboratory</span></button>
  <button><span>Each button must be as wide as the widest one</span></button>
</div>

button {padding:0;}
button span {margin:1em;}

编辑
这里发生的事情不止一件事。第二个问题是 ems。显然,当文本容器的宽度呈现为 328.00003654 像素时,文本换行可能会出错。为 span {margin: 切换到像素单位修复了这个问题。

关于html - Border-Box 100% 宽度计算 - 文本不适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12609814/

相关文章:

javascript - 如何确保输入不为空?

html - Bootstrap Navbar 卡在移动设备上

CSS 媒体查询流体图像

javascript - 使用更改图像隐藏和显示 div

ios - 如何在 ios 中点击按钮时实现日期选择器?

html - 新的 xfbml "Like"按钮 + CloudFlare 503 错误杀死计数?

php - 如何限制运行 DOMDocument->loadHtmlFile 时的超时?

html - 当菜单有垂直滚动条时显示菜单项外的子菜单

php - Python : extract . 将数据提交到具有机械化的表单后的 csv 结果

mysql - 分页和表单标签问题