views:

1003

answers:

2

Does anyone know why the input elements with a width of 100% go over the table's cells border. In the simple example below input box go over the table's cells border, the result is horrible. This was testet and it happens in the same way on: FF, IE7 and Safari. Does it make sense for you? Am I missing something, do you know about a possible solution?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;    
<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>
+3  A: 

Width value doesn't take into account border or padding:

http://www.htmldog.com/reference/cssproperties/width/

You get 2px of padding in each side, plus 1px of border in each side. 100% + 2*(2px +1px) = 100% + 6px, which is more than the 100% child-content the parent td has.

Fortunately for me you didn't ask for a solution, just an explanation. :P I don't know a solution.

(If there wasn't border in the input, you could simply put padding on the td and it'd collapse with the input's padding.)

ANeves
further to Sr pts answer you could set the padding and border to 0px then the 100% should work.
Mauro
Indeed, even without setting any padding for td - very well added.
ANeves
Also, if the padding creates an issue, it's possible to use `text-indent` to simulate padding ahead of the leading-edge of the text, and `line-height` for vertical padding (though retaining vertical padding wouldn't affect the width anyway).
David Thomas
A: 

The problem lies in border-width of input element. Shortly, try setting the margin-left of the input element to -2px.

table input { margin-left: -2px; }