Larsenal
Larsenal

Reputation: 51156

Problem with <input type='text' /> and <textarea> width

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>

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.

Upvotes: 9

Views: 18777

Answers (6)

bmaupin
bmaupin

Reputation: 15995

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

How can I make a TextArea 100% width without overflowing when padding is present in CSS?

Here's what it looked like for me:

<style>
   .mywidth{ 
     width:100%;
     -moz-box-sizing: border-box;
     -ms-box-sizing: border-box;
     -webkit-box-sizing: border-box;
     box-sizing: border-box;
    } 
</style>

Upvotes: 13

Rainner
Rainner

Reputation:

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.

Upvotes: 0

philnash
philnash

Reputation: 73027

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.

Upvotes: 7

Gabe Hollombe
Gabe Hollombe

Reputation: 8067

@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.

Upvotes: 1

Toby Mills
Toby Mills

Reputation: 1016

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.

Upvotes: 0

Jaime Garcia
Jaime Garcia

Reputation: 7096

You could try using this DOCTYPE instead

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Upvotes: 0

Related Questions