I would like an input[type=text] element to always be the same width as the table cell it is contained in. I need to ensure that the width of the table cell is not influenced by the width of the input element.
How can I do this with CSS? Will I need to use JS? If so, what would be the best way to determine when the table cell changed size?
The width attribute specifies the width of the <input> element. Note: The width attribute is used only with <input type="image"> . Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.
The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.
To adjust column width automatically, click AutoFit Contents. To adjust table width automatically, click AutoFit Window.
if this does work for you:
td > input[type='text']
{
width: 100%; /*this will set the width of the textbox equal to its container td*/
}
try this:
td > input[type='text']
{
display:block; /*this will make the text box fill the horizontal space of the its container td*/
}
Setting CSS rules width:100%;
and display:block;
for the INPUT
element should do that.
In practice, that is not enough, it seems some browsers emphasize the INPUT
's size
attibute over the style. So for example, Chrome widens the element to agree with size, thus widening the table column. Setting size=0
doesn't work, as it is defaulted to some valid value then. However, setting size=1
attribute at the INPUT
element achives the desired behaveour. However, this is no CSS-only solution, and the INPUT
cannot be compressed below a certain but small width.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With