有人知道为什么宽度为100%的输入元素会越过表格的单元格边框吗?

在下面的简单示例中,输入框越过表格的单元格边框,结果非常糟糕.这是经过测试的,在Firefox、IE7和Safari上也是如此.

这对你来说有意义吗? 我是不是漏掉了什么,你知道可能的解决方案吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
<html><head>    
   <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <!-- don't use closing slash in meta tag, it breaks HTML4.01 transitional -->
   <title>Test input text in table</title>
   <style type="text/css">      
      table {border-top: 1px solid #ff0000; border-left: 1px solid #ff0000;}
      table td {border-right: 1px solid #00ff00; border-bottom: 1px solid #00ff00;}
      input[type="text"] {width: 100%;} /* removing this would make input not to go over cells border, but they would be too short, I want them to fit cells size */
   </style>    
</head><body>

<table cellpadding="0" cellspacing="0">
   <tr>
      <td><p>column one hello babe babe babe</p></td>
      <td><p>column two hello babe more</p></td>
      <td><p>column three hello babe more and more</p></td>
   </tr>
   <tr>
      <td><input type="text" value="test"></td>
      <td><input type="text" value="test"></td>
      <td><input type="text" value="test"></td>
   </tr>
</table>

</body></html>

推荐答案

您可以使用CSS3 box-sizing属性包括外部填充和边框:

input[type="text"] {
     width: 100%; 
     box-sizing: border-box;
     -webkit-box-sizing:border-box;
     -moz-box-sizing: border-box;
}

Html相关问答推荐

试图让三个Divs与下面的另外三个对齐

将ANGLE模板高效地迁移到ANGLE 17中引入的新语法

如何从卷轴中排除粘性元素?

使用响应迅速的网格系统,将两列保持在同一行,同时允许更多列用于更大的屏幕

Django HTML标记-Merge for Loop with Conditional语句

希望平稳过渡到悬停状态

当浏览器宽度减小时字体大小变得太大

调整 Rmarkdown 输出的 html_document 的标题和副标题之间的间距

停止 Bootstrap 5 输入调整 CSS 网格单元的高度

伪元素:after和溢出隐藏

jQuery 索引返回不正确的结果

理解图像与其内容框之间的关系

如何从 div 内的图像中删除填充而不从其他所有内容中删除填充?

XPATH Select h2标签和

标签之间或h2标签和 标签之间的td元素,以立即为准

pandas `to_html()` - 如何只使特定的行有边框

如何清除html输入中的文本

Css 左属性问题

如果不透明的元素出现在下方,为什么不透明度会使一个元素出现在另一个元素上方?

当另一个 div 的高度改变或执行其内容的换行时,将 flex 方向更改为列

文本输入在 Chrome 中保持水平滚动,带有文本溢出:省略号