Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have an input element fill the width of a table column?

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?

like image 234
diolemo Avatar asked Dec 23 '11 05:12

diolemo


People also ask

How do you write input to width?

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.

Can I set table column width?

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.

How do you increase cell width in a table?

To adjust column width automatically, click AutoFit Contents. To adjust table width automatically, click AutoFit Window.


2 Answers

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*/
}
like image 82
jerjer Avatar answered Sep 17 '22 18:09

jerjer


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.

like image 34
dronus Avatar answered Sep 17 '22 18:09

dronus