在下面的代码中,INPUT 和 TEXTAREA 元素呈现的宽度都超出了应有的宽度。如何将它们限制为 div 内可用区域的 100%?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
.mywidth{ width:100%; }
</style>
</head>
<body>
<div style="border: 3px solid green; width: 100px;">
<input class="mywidth" ><br />
<textarea class="mywidth"></textarea><br />
<div style="background-color: yellow;" class="mywidth">test</div>
</div>
</body>
</html>
注意:如果我删除 DOCTYPE,它会按预期呈现,INPUT、TEXTAREA 和内部 DIV 的宽度都相同,并且不会超出包含的 DIV。
更新:尽管这些元素上有默认边框,它在 IE7 中似乎仍然呈现不正确。
最佳答案
我遇到了同样的问题。我使用了此处提到的 box-sizing 属性:
How can I make a TextArea 100% width without overflowing when padding is present in CSS?
这对我来说是这样的:
<style>
.mywidth{
width:100%;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
</style>
关于html - <input type ='text'/> 和 <textarea> 宽度有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/259455/