tags:

views:

4930

answers:

7

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"&gt;
<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>

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.

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

A: 

You could try using this DOCTYPE instead

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
ferrari fan
Doesn't appear to fix it.
Larsenal
Not having a DOCTYPE will force the browser into quirks mode, using the old, incorrect box model which doesn't coun't the border as part of the width.
Phil
Or... `<!DOCTYPE html>` :P
Delan Azabani
+4  A: 

Inputs and textareas both have borders by default

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

will render all the elements within your container.

Update

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>

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.

Phil
Awesome, Phil. I never knew this.
Gabe Hollombe
Have you tried it? Check IE7. It still bites into the green border on the containing div.
Larsenal
Apologies, I'm on a Mac! I'll see what I can do in IE7.
Phil
A: 

Add "padding-right:3px;" to the div so it reads as:

<div style="border: 3px solid green;padding-right:3px; width: 100px;">

Because you have added a border to the div that also counts as internal space of the div.

The reason it works without the doc declaration is that the browser does not render the page as transitional XHTML but plain old html which has a different rendering method for div's etc.

Toby Mills
+1  A: 

@Phil has the answer, above.

Incidentally, using Firebug does, indeed, show the default borders on the textarea and input elements. So, using Firebug might have helped.

Gabe Hollombe
It does look fine in FF, but it's still rendering incorrectly in IE7.
Larsenal
A: 

nice, just tell the with with px and thats all

A: 

xhtml strict code seems to do that with forms. i just make the inputs and textareas stretch at 99% and that seems to work. give that a try.

+1  A: 

I had this same problem. I used the box-sizing property mentioned here:

http://stackoverflow.com/questions/271067/how-can-i-make-a-textarea-100-width-without-overflowing-when-padding-is-present#answer-2515439

Here's what it looked like for me:

<style>
   .mywidth{ 
     width:100%;
     box-sizing: border-box;
     -moz-box-sizing: border-box;
     -webkit-box-sizing: border-box;
    } 
</style>
Bryan
Perfect! Thanks!!!
Brandon Boone