简体   繁体   English

宽度为100%的Textarea会填充容器,但宽度为100%的div会扩展父级

[英]Textarea at 100% width fills container but div at 100% width expands the parent

I'm having an interesting problem that I'm hoping someone can help with. 我遇到了一个有趣的问题,希望有人可以提供帮助。 I have a textarea that when set at 100% width, fills the parent div that it's in. HOWEVER, if I replace that with a div, the div blows out the parent divs width to now fit the child's content instead of wrapping. 我有一个textarea,当设置为100%宽度时,将填充它所在的父div。但是,如果我将其替换为div,则div会炸掉父div的宽度,以适合孩子的内容而不是包装。

I've tried various approaches (display:inline-block, display:table, display:table-cell, box-sizing...) I've used span, div, p and table/tr/td.. all result in the following overflow. 我尝试了各种方法(display:inline-block,display:table,display:table-cell,box-sizing ...)我使用了span,div,p和table / tr / td。以下溢出。

文字区域

Div标签

盒大小src

I've inspected the source, and all I see is the textarea's width being set to 100% with no other styles affecting it. 我检查了源,我所看到的只是将textarea的宽度设置为100%,没有其他样式影响它。

Is there's an attribute I'm missing when I'm inspecting? 检查时是否缺少属性?

我的猜测是,您只需要利用边框

*{ box-sizing: border-box; }

Basically, your div might have padding also. 基本上,您的div可能也有padding Now when you add the padding with width: 100% , the child will basically overflow the parent cause of the extra padding. 现在,当您添加width: 100%的填充时,孩子基本上会溢出导致多余填充的父级原因。

To include padding within the width try providing your div a box-sizing: border-box; 要在宽度内包括填充,请尝试为div提供一个box-sizing: border-box; .

You can read more about box-sizing here . 您可以在此处阅读有关框大小的更多信息。

Due to the complexity of the application, I'll need to strip it down to the basics and figure it out. 由于应用程序的复杂性,我需要将其精简为基础并弄清楚。 I was hoping that there was a structural component of the textarea that I didn't know about. 我希望有一个我不知道的文本区域的结构部分。

Thanks, 谢谢,

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

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