简体   繁体   English

保留左边和宽边的div:右侧溢出100%

[英]Div with margin-left and width:100% overflowing on the right side

I have 2 nested div's that should be 100% wide. 我有2个嵌套的div应该是100%宽。 Unfortunately the inner div with the Textbox overflows and is actually larger than the outer div. 不幸的是,带有Textbox的内部div溢出并且实际上比外部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. 如果我不做100%,那么文本框不是100%宽。

Just remove the width from both divs. 只需从两个div中删除宽度即可。

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. div是块级元素,将使用所有可用空间(除非您开始浮动或定位它们),因此外部div将自动为100%宽,内部div将在设置左边距后使用所有剩余空间。

I have added an example with a textarea on jsfiddle . 我在jsfiddle上添加了一个带textarea例子

Updated example with an input. 带输入的更新示例

A div is a block element and by default 100% wide. div是块元素,默认为100%宽。 You should just have to set the textarea width to 100%. 您只需将textarea宽度设置为100%即可。

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. 这里给出的其他答案都没有给我工作,我已经放弃了希望,但今天我正在寻找解决另一个类似问题的解决方案,我在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! 接受的答案适用于我的div,我突然想到尝试使用我以前的文本框问题 - 它有效! The solution: 解决方案:

add box-sizing: border-box to the style of the textbox. box-sizing: border-box到文本框的样式。

To add this to all multi-line textboxes using CSS, add the following to your style sheet: 要使用CSS将其添加到所有多行文本框,请将以下内容添加到样式表中:

textarea
{
  box-sizing: border-box;
}

Thanks to thirtydot for the solution at 感谢thirtydot提供的解决方案

width: 100%-padding? 宽度:100%-padding?

and

Content of div is longer then div itself when width is set to 100%? 当宽度设置为100%时,div的内容比div本身更长?

<div style="width:100%;">
    <div style="margin-left:45px;">
       <asp:TextBox ID="txtTitle" runat="server" Width="100%"></asp:TextBox><br />
    </div>
</div>

If some other portion of your layout is influencing the div width you can set width:auto and the div (which is a block element) will fill the space 如果布局的某些其他部分影响div宽度,则可以设置width:autodiv (这是一个块元素)将填充空间

<div style="width:auto">
    <div style="margin-left:45px;width:auto">
        <asp:TextBox ID="txtTitle" runat="server" Width="100%"></asp:TextBox><br />
    </div>
</div>

If that's still not working we may need to see more of your layout HTML/CSS 如果仍然无法正常工作,我们可能需要查看更多布局HTML / CSS

Add some css either in the head or in a external document. 在头部或外部文档中添加一些css。 asp:TextBox are rendered as input : asp:TextBox呈现为输入:

input {
     width:100%;
}

Your html should look like : http://jsfiddle.net/c5WXA/ 你的HTML应该是这样的: 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的类提供一个类并指定css。

.divClass input {
     width:100%;
}

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

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