带 margin-left 和 width:100% 右侧溢出的 Div
我有 2 个嵌套的 div,宽度应该是 100%。不幸的是,带有文本框的内部 div 溢出,并且实际上比外部 div 更大。它有一个左边距,并且溢出了大约该边距的大小。
我该如何解决这个问题?
<div style="width:100%;">
<div style="margin-left:45px; width:100%;">
<asp:TextBox ID="txtTitle" runat="server" Width="100%"></asp:TextBox><br />
</div>
</div>
如果我不做 100%,那么文本框就不是 100% 宽。
I have 2 nested div's that should be 100% wide. Unfortunately the inner div with the Textbox overflows and is actually larger than the outer div. It has a left margin and overflows by about the size of the margin.
How can I fix that?
<div style="width:100%;">
<div style="margin-left:45px; width:100%;">
<asp:TextBox ID="txtTitle" runat="server" Width="100%"></asp:TextBox><br />
</div>
</div>
If I don't do the 100%, then the textbox is not 100% wide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需删除两个 div 的宽度即可。
div
是一个块级元素,将使用所有可用空间(除非您开始浮动或定位它们),因此外部 div 将自动为 100% 宽,内部 div 将使用设置后的所有剩余空间左边距。我在 jsfiddle 上添加了一个带有
textarea
的示例。使用输入更新了示例。
Just remove the width from both divs.
A
div
is a block level element and will use all available space (unless you start floating or positioning them) so the outer div will automatically be 100% wide and the inner div will use all remaining space after setting the left margin.I have added an example with a
textarea
on jsfiddle.Updated example with an input.
div 是块元素,默认情况下为 100% 宽。您只需将文本区域宽度设置为 100% 即可。
A div is a block element and by default 100% wide. You should just have to set the textarea width to 100%.
如果布局的其他部分影响
div
宽度,您可以设置width:auto
并且div
(这是一个块元素)将填充空间如果仍然不起作用,我们可能需要查看您的布局 HTML/CSS 的更多内容
If some other portion of your layout is influencing the
div
width you can setwidth:auto
and thediv
(which is a block element) will fill the spaceIf that's still not working we may need to see more of your layout HTML/CSS
在 head 或外部文档中添加一些 css。 asp:TextBox 作为输入呈现:
您的 html 应如下所示: http://jsfiddle.net/c5WXA/
请注意,这将影响您的所有文本框:如果您不希望这样,请为包含的 div 指定一个类并指定 css。
Add some css either in the head or in a external document. asp:TextBox are rendered as input :
Your html should look like : http://jsfiddle.net/c5WXA/
Note this will affect all your textbox : if you don't want this, give the containing div a class and specify the css.
我意识到这是一篇旧文章,但这可能会让那些像我一样通过谷歌搜索来到此页面并且束手无策的人受益。
这里给出的其他答案都不适合我,我已经放弃了希望,但今天我正在寻找 div 的另一个类似问题的解决方案,我发现这个问题在 SO 上得到了多次回答。接受的答案适用于我的 div,我突然想到尝试解决我之前的文本框问题 - 它有效!
解决方案:
在文本框的样式中添加
box-sizing: border-box
。要将其添加到使用 CSS 的所有多行文本框,请将以下内容添加到样式表中:
感谢 thirddot 在
宽度:100%-padding?
和
当宽度设置为100%时,div的内容比div本身更长?
I realise this is an old post but this might benefit somebody who, like me, has come to this page from a google search and is at their wits end.
None of the other answers given here worked for me and I had already given up hope, but today I was searching for a solution to another similar problem with divs, which I found answered multiple times on SO. The accepted answer worked for my div, and I had the sudden notion to try it for my previous textbox issue - and it worked!
The solution:
add
box-sizing: border-box
to the style of the textbox.To add this to all multi-line textboxes using CSS, add the following to your style sheet:
Thanks to thirtydot for the solution at
width: 100%-padding?
and
Content of div is longer then div itself when width is set to 100%?