简体   繁体   English

设置表格内文本区域的高度

[英]Setting the height of a textarea inside a table

I've set the height of my textarea to 500%, but for some reason it's not changing to 500%.我已将 textarea 的高度设置为 500%,但由于某种原因,它没有更改为 500%。 I think it has something to do with it being inside a table, but I'm not sure what to change set the height correctly.我认为这与它在桌子内有关,但我不确定如何更改正确设置高度。 For some reason, the width of the textarea CAN be change inside the table.出于某种原因,文本区域的宽度可以在表格内更改。

 table,td { border: 1px solid black; } textarea { resize: none; width: 100%; height: 500%; }
 <table> <tr> <td> firstTD </td> <td> <form method = 'POST' action = 'updateProfile.php'> <textarea id = 'textarea' placeholder = 'type something here...' onfocus = \\"this.placeholder = ''\\" onblur = \\"this.placeholder = 'type something here...'\\" maxlength = '10000'></textarea> </form> </td> </tr> </table>

The other answer solves the problem, but doesn't explain it.另一个答案解决了问题,但没有解释。 When you use % to define width/height in CSS, you are making it whatever% of that element's container' width/height.当您在 CSS 中使用 % 定义宽度/高度时,您将其设置为该元素容器宽度/高度的任何百分比。

When you set your textarea to be 100% of an otherwise empty <td> , it's not going to be very big.当您将 textarea 设置为 100% 的空<td> ,它不会很大。

Setting it to posistion:absolute will work IF none the ancestor elements are positioned.如果没有定位祖先元素,将其设置为 position:absolute 将起作用。 A simpler approach would be to just use something other than % to define your width and height.一种更简单的方法是使用 % 以外的其他东西来定义宽度和高度。 Try width:10em;尝试width:10em; and adjust it until you get it right.并调整它直到你做对了。

Edit.编辑。

To answer a question in the comments: The reason using % to define the height works in this case, is because the empty cell has a height, but not a width.在评论中回答一个问题:在这种情况下使用 % 定义高度的原因是因为空单元格有高度,但没有宽度。 An empty table cell still inherits the height of the row, which is as tall as the tallest <td> .空表格单元格仍继承行的高度,该高度与最高的<td>一样高。 In this case there is another <td> that has content, giving the cell a height.在这种情况下,还有另一个<td>有内容,给单元格一个高度。

So, If there was another row, and one of the cells in the same column had content, then width would work with % too.因此,如果还有另一行,并且同一列中的一个单元格有内容,那么宽度也可以与 % 一起使用。

That said, it's not a good idea to use % for width and height in a table, because when the content in the table changes, your percentages will change.也就是说,在表格中使用 % 表示宽度和高度并不是一个好主意,因为当表格中的内容发生变化时,您的百分比也会发生变化。 Also, when you use % as opposed to px or em, it will not stretch the parent container.此外,当您使用 % 而不是 px 或 em 时,它不会拉伸父容器。

Edit again再次编辑

I honestly didn't even notice the form element.老实说,我什至没有注意到表单元素。 Then your percents are relative to the height/width of the form element, not the <td> .然后你的百分比是相对于表单元素的高度/宽度,而不是<td> There must be some padding giving your cells width/height but the form wouldn't have any dimensions.必须有一些填充为您的单元格提供宽度/高度,但表单没有任何尺寸。

Try setting position: absolute to textarea and give a position: relative to the parent.尝试将position: absolute设置为textarea并给出一个position: relative对于父级。 Also remove width and give left and right values as 0 .还除去width和给leftright值作为0 But remember, this will make the textarea to overflow out of the content.但请记住,这会使textarea溢出内容。 Is that what you are expecting?这是你所期待的吗?

 table, td { border: 1px solid black; position: relative; width: 350px; } textarea { resize: none; height: 500%; left: 0; right: 0; position: absolute; top: 0; }
 <table> <tr> <td> firstTD </td> <td> <form method='POST' action='updateProfile.php'> <textarea id='textarea' placeholder='type something here...' onfocus=\\ "this.placeholder = ''\\" onblur=\\ "this.placeholder = 'type something here...'\\" maxlength='10000'></textarea> </form> </td> </tr> </table>

Or if you need something like this?或者如果你需要这样的东西?

 table, td { border: 1px solid black; position: relative; width: 350px; } textarea { resize: none; height: 10em; width: 100%; display: block; box-sizing: border-box; }
 <table> <tr> <td> firstTD </td> <td> <form method='POST' action='updateProfile.php'> <textarea id='textarea' placeholder='type something here...' onfocus=\\ "this.placeholder = ''\\" onblur=\\ "this.placeholder = 'type something here...'\\" maxlength='10000'></textarea> </form> </td> </tr> </table>

With this you can set size for text area有了这个,您可以设置文本区域的大小

$("textarea").css("height", $("textarea").parent("td").height())
.css("width", $("textarea").parent("td").width())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM