问题与< input type ='text'/>和< textarea>宽度 [英] Problem with <input type='text' /> and <textarea> width

查看:144
本文介绍了问题与< input type ='text'/>和< textarea>宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,INPUT和TEXTAREA元素的呈现比它们应该更宽。

In the following code, both the INPUT and TEXTAREA elements render wider than they should. How can I limit them to 100% of the usable area within the div?

<!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和inner DIV

Note: If I remove the DOCTYPE, it renders as expected, with the INPUT, TEXTAREA and inner DIV all the same width and not going outside the containing DIV.

更新:不考虑这些元素的默认边框,它仍然出现在 IE7

Update: Not withstanding the default borders on those elements, it still appears to render incorrectly in IE7.

推荐答案

输入和文本区域默认都有边框

Inputs and textareas both have borders by default

<style>
   .mywidth{ 
     width:100%;
     border:0;
    } 
</style>

将呈现容器中的所有元素。

will render all the elements within your container.

更新

IE在每个元素上都有左侧和右侧填充,下面的css适合FF3中容器中的所有元素, FF2,Safari 3,IE6和IE7。

IE also has left and right padding on each element and the following css fits all the elements within the container in FF3, FF2, Safari 3, IE6 and IE7.

<style>
   .mywidth{ width:100%; border:0; padding-left:0; padding-right:0; }
</style>

但是,不要忘记你可能需要一个边框,命令使字段对用户显示为正常。如果你自己设置边框和填充,那么你会知道在不同浏览器之间,容器的宽度和你需要给输入/ textarea元素的宽度之间的区别是什么。

However, don't forget that you will probably need a border, and perhaps the padding too, in order to make the fields appear to users as normal. If you set that border and padding yourself then you will know what the difference is, across browsers, between the width of the container and the width you will need to give to the input/textarea elements.

这篇关于问题与&lt; input type ='text'/&gt;和&lt; textarea&gt;宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆