简体   繁体   English

我可以在表格列的宽度上填充输入元素吗?

[英]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. 在实践中,这还不够,似乎有些浏览器强调INPUTsize不同于风格。 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.

相关问题 将输入宽度绑定到表格列宽度 - Bind input width to table column width 如何将“ fill_parent”值设置为元素的宽度? - How can I set “fill_parent” value as an element's width? CSS / HTML 表:如何根据输入元素的文本值自动增长列宽 - CSS / HTML Table: How to autogrowth a column width based on the text value of the input element 如何使用javascript或jQuery使此表填充可用的宽度和高度? - How can I use javascript or jQuery to make this table fill available width and height? 我无法使用element.style.width获得Div的宽度。 我必须改用element.clientWidth吗? - I can't get a Div's width using element.style.width. Do I have to use element.clientWidth instead? React Bootstrap Table - 尝试用相等的列宽填充页面 - React Bootstrap Table - Trying To Fill Page With Equal Column Width 我们可以有一条带有笔触,填充和笔触宽度的SVG线吗 - Can we have a SVG line with stroke, fill and stroke-width 当我分配的列宽等于表格宽度除以列数时,为什么会出现水平滚动? - Why do I have horizontal scroll when I assign column width equal to table width divide by number of columns? 表格元素中的输入字段:我可以将其分配给类吗? - Input field in a table element: can I assign it to a class? 如何获取表格标题的宽度并与 JQuery 中的表格单元格具有相同的大小? - How can I get the width of the table header and have the same size as the table cell in JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM