[英]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. 我希望input [type = text]元素的宽度始终与它所包含的表格单元格的宽度相同。我需要确保表格单元格的宽度不受输入元素宽度的影响。
How can I do this with CSS? 我怎么能用CSS做到这一点? Will I need to use JS? 我需要使用JS吗? If so, what would be the best way to determine when the table cell changed size? 如果是这样,那么确定表格单元何时改变大小的最佳方法是什么?
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%;
设置CSS规则width:100%;
and display:block;
和display:block;
for the INPUT
element should do that. 对于INPUT
元素应该这样做。
In practice, that is not enough, it seems some browsers emphasize the INPUT
's size
attibute over the style. 在实践中,这还不够,似乎有些浏览器强调INPUT
的size
不同于风格。 So for example, Chrome widens the element to agree with size, thus widening the table column. 因此,例如,Chrome扩展了元素以符合大小,从而扩大了表格列。 Setting size=0
doesn't work, as it is defaulted to some valid value then. 设置size=0
不起作用,因为它默认为某个有效值。 However, setting size=1
attribute at the INPUT
element achives the desired behaveour. 但是,在INPUT
元素中设置size=1
属性可以获得所需的行为。 However, this is no CSS-only solution, and the INPUT
cannot be compressed below a certain but small width. 但是,这不是仅限CSS的解决方案,并且INPUT
不能压缩到一定宽度以下。
Setting width of your field to 100% should work. 将字段宽度设置为100%应该有效。 In this case you will also want to add box-sizing
rule to avoid the input field from over ignoring table cell paddings. 在这种情况下,您还需要添加box-sizing
规则,以避免输入字段忽略表格单元格填充。
td.value input {
width: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
See example http://jsfiddle.net/MEARG/ 参见示例http://jsfiddle.net/MEARG/
What about setting style (or class) width: 100%; 如何设置样式(或类)宽度:100%; for input elements that are inside a table? 对于表内的输入元素?
Something like this: 像这样的东西:
td input.myclass[type=text]
{
width: 100%;
}
CSS should do the trick: CSS应该做的伎俩:
#my_input {
width: 100%;
}
#my_table {
table-layout:fixed;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.